A question about module/imports/exports
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
On Mon, Aug 27, 2012 at 2:11 PM, Luke Hoban <lukeh at microsoft.com> wrote:
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'.
Sorry I don't get it. Do you mean
import {a} in A // compile error?
but
A.a // accessible?
On 27 August 2012 11:46, Shijun He <hax.sfo at gmail.com> wrote:
On Mon, Aug 27, 2012 at 2:11 PM, Luke Hoban <lukeh at microsoft.com> wrote:
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'.
Sorry I don't get it. Do you mean
import {a} in A // compile error?
but
A.a // accessible?
No, Luke just meant that the assignment in changeA should read
_a = v
since that is the local name of the variable. After changing that, the module is indeed equivalent to the other version.
On Mon, Aug 27, 2012 at 5:55 PM, Andreas Rossberg <rossberg at google.com> wrote:
No, Luke just meant that the assignment in changeA should read
_a = v
Oh, my typo!
since that is the local name of the variable. After changing that, the module is indeed equivalent to the other version.
/Andreas
Thank you.
-- hax
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'?
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} }
Thank you!
-- hax