Exporting objects and classes before declaring them makes them undefined

I am trying to repeat an example from Mozilla Hacks ( Export Lists ):

//export.js
export {detectCats, Kittydar};
function detectCats() {}
class Kittydar {}

//import.js
import {detectCats, Kittydar} from "./export.js";
console.log(detectCats); // function detectCats() {}
console.log(Kittydar); // undefined

Unfortunately, Kittydar is undefined (by the way, the problem is the same with simple Object).

But if I put it exportafter the declaration Kittydar, this is normal:

//export.js
class Kittydar {}
export {Kittydar};

//import.js
import {Kittydar} from "./export.js";
console.log(Kittydar); // function Kittydar() {_classCallCheck(this, Kittydar);}

Is this a typo in the article?

I will pass this with the help babeland bundles with browserify. Then I include the output package in a regular .htmlfile (with tag <script>).

+4
source share
1 answer

, . es6draft SpiderMonkey: , , console.log.

, :

  • JS import.js. import, export.js .

  • - , , . ( ModuleDeclarationInstantiation.) export.js Kittydar, .

    import.js Kittydar. Kittydar export.js.

  • export.js. . Kittydar .

  • import.js. console.log() .


ES6 Babel .

, . Babel ES6 ES5-, : AMD, UMD, CommonJS .. , AMD, ES6, ES5 AMD, AMD.

, , , , .

+5

Source: https://habr.com/ru/post/1622858/


All Articles