0
votes

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

1
"where can I find the usage intro to this specific syntax " What exactly do you mean? Do you understand what the syntax does but do not understand why somebody would want to use this?Felix Kling
"why I can not declare a variable and export it" Because default exports syntax simply doesn't allow this. Do export default { hello: ... }; or var Obj = { ... }; export default Obj.Felix Kling
@FelixKling thanks, for "where can I find...", I do not know what that syntax means, I read some ES6 tutorial, but can not find a post talk about official syntax like { each as forEach }, does this line mean doing a named export and using "name1 as name2 wrapped in curly bracket" syntax to do a name aliasing just like var forEach; forEach = each;?Kuan
From the page you linked to, point 5.2 "You can also export things under different names"Felix Kling

1 Answers

1
votes

[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 };