Why is "export default var a = 1;" invalid syntax?

Brendan Eich brendan at mozilla.org
Mon Dec 15 22:00:28 PST 2014


Glen Huang wrote:
> And after reading both of your responses, my question actually becomes: is it correct that there is not way to rebind the value being exported via the “export default” declaration, unless you use something like "export { a as default };"

I think that's right. Or by brute-force reflection on the module object, 
to hack the property named 'default' in it.

> Does it also mean that:
>
> ```js
> export default function a() {}
> a = 2;
> ```
>
> The assignment "a = 2” will result in an error, since "function a() {}” is an expression, so “a” was never defined at that point?

In strict code, unless there was already an 'a' binding in scope, that 
would indeed be a runtime error. Modules are strict by definition.

>  From esthetic point of view, “a" being not bound in the previous snippet really surprises me, considering its “twin" version:
>
> ```js
> export function a() {}
> a = 2;

Yup, wherefore Dave's original syntax design interposing an '=' before 
the 'function a() {}' to make clear the latter is an expression.

It's a pity the '=' isn't required. Maybe it can be an ES7 optional 
syntax extension, but at that point we'd regret not requiring it -- so 
either ES6 should be revised ASAP to require it, or we should drop the 
idea and learn to live with the unclear expression context.

> If I understand the spec correctly, this should cause the “a” export in the binding be 2.

Yes, just like in any other JS scope, function declarations make 
writable bindings.

/be


More information about the es-discuss mailing list