Kuan
Kuan

Reputation: 11389

ES6 syntax confuse about module export

All:

I am pretty new to ES6, when I study ES6 module with this post:

http://www.2ality.com/2014/09/es6-modules-final.html

[1]

there is one line:

export { each as forEach };

I wonder if anyone could tell where can I find the usage intro to this specific syntax (especially the curly bracket and AS )

[2]

Another confuse is about default export:

cmod.js

export default var Obj={};
Obj.hello =  function(){
    return "hello";
}

main.js

import hello from "./cmod"
console.log(hello.hello());

I wonder why I got error like: :

SyntaxError: cmod.js: Unexpected token (1:15)
> 1 | export default var Obj={};
    |                ^

But if I move the declaration to a separated line like:

var Obj = {}
export default Obj;
Obj.hello =  function(){
    return "hello";
}

Then everything works, why I can not declare a variable and export it?

Thanks

Upvotes: 0

Views: 54

Answers (1)

Galeel Bhasha
Galeel Bhasha

Reputation: 1895

[1] export { each as forEach }; this line means, you can export one or more items as objects with or without alias names.

example-1:

const MY_CONST = ...;
   function myFunc() {
    ...
}

export { MY_CONST, myFunc };

example-2:

export { MY_CONST as THE_CONST, myFunc as theFunc };

Upvotes: 1

Related Questions