Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES6 Export Overwriting Function

How can I export this overwriting function so that an importing module can check whether the function has been called?

// util.js
export function isPageload() {
  return (!!(isPageload = function() { return false; }));
}

When I compile that with Babel, I get this error:

Uncaught TypeError: (0 , _util2.default) is not a function

Here is the ES5 equivalent:

var isPageload = function() {
  return (!!(isPageload = function() { return false; }));
}

console.log(isPageload()); // true
console.log(isPageload()); // false
like image 690
cantera Avatar asked Sep 26 '15 03:09

cantera


1 Answers

The .default in the error makes it clear that you are doing

import isPageload from 'foo';

when you probably want

import {isPageload} from 'foo';

since

export function isPageload() {

creates a named export, not a default export, and default export live-binding updating currently does not work in Babel.

Your approach to this problem does seem somewhat roundabout however. Why not do

let loaded = true;
export isPageLoaded(){
    let state = loaded;
    loaded = false;
    return loaded;
}
like image 110
loganfsmyth Avatar answered Oct 06 '22 23:10

loganfsmyth