A question about module/imports/exports

Luke Hoban lukeh at microsoft.com
Sun Aug 26 23:11:23 PDT 2012


> Hi, I'm writing a library for ES5 to add module system like ES6, I just want to confirm some behaviors of ES6 module system:

> module A {
>   export var a = 'a'
>   export function changeA(v) {
>      a = v
>   }
> }

> module B {
>  import * from A
>   console.log(a) // 'a'
>   changeA('a1')
>   console.log(a) // 'a' or 'a1' ?
> }

> Current CommonJS/AMD module system only can return 'a', but I guess
ES6 should return 'a1'?

That's right, the modules proposal exports accessors which can refer to captured module local variables.  This is roughly equivalent to what could be done in CommonJS today with something like:

// a.js
Object.defineProperty(exports, "a", {get: function() { return a; }})
Object.defineProperty(exports, "changeA", {get: function() { return changeA; }})
var a = 'a'
function changeA(v) {
   a = v
}

> And is there any difference if module A is write as:

> module A {
>   var _a = 'a'
>   export function changeA(v) {
>      a = v
>   }
>   export {a: _a}
> }

This should not work.  There is no value in scope inside the module 'A' with the name 'a' here.  The exported name 'a' is only available as a member of the module 'A' via 'A.a'.  

Luke




More information about the es-discuss mailing list