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

Brendan Eich brendan at mozilla.org
Mon Dec 15 22:06:11 PST 2014


Ah, thanks -- I remember someone pointing this out, now that you mention it.

My misunderstanding in the last two posts dings my ES6 quiz score! Who 
will get a perfect score in a fresh (uncoached) quiz setting? Is this 
telling us something? Anyway, I agree that `export default function a() 
{}` making an `a` binding is better, it resolves the confusing aspect in 
my misunderstood version of ES6 that Glen cited in his 7pm post.

One more question below:

Allen Wirfs-Brock wrote:
>> This seems clear. As Dave said, he originally proposed an '=' in 
>> between 'default' and the *expression* to evaluate on the right. That 
>> design remembrance should make clear that the default export is a 
>> function expression (not function declaration) with 'a' the name only 
>> in the scope of that function (either for recursion or as a downward 
>> funarg).
>>
>> The 'default' binding won't be mutated via the final 'a = 2' 
>> statement, so the default-exported value is still the result of 
>> evaluating the function a(){} expression.
>
> Not quite how it actually ended up.  See 
> https://bugs.ecmascript.org/show_bug.cgi?id=2302 for background.
>
> export default function ...
> export default function  * ...
> export default class ...
>
> all act as declaration that create a module local binding (for the 
> name in the declaration part)  that is initialized in the normal 
> manner (hoisted for function/function*,  statement order 
> initialization for class).  In addition that binding is exported using 
> the reserved export name 'default'. Just like, an export of the same 
> declaration without 'default', except such declaration use the same 
> name for both the export name and the local binding name.

So assigning `a = 2` won't affect the value of the default export, but 
will rebind `a`, given preceding `export default function a() {}` -- right?

> For export default, if the declaration is anonymous (this required 
> some minor syntax tweaking) , a local binding is still created (and 
> initialized in the manner manner) but the local binding isn't locally 
> referencable because it doesn't have a name that can be referenced via 
> an IdentifierReference.

Hmm, how's that work? Throwaway symbol name?

I ask because Glen also wondered about the Module Record being reified 
in one of his posts. That's spec-internal, though (again, correction 
welcome).


> If you want to export the value of a named 
> FunctionExpression/GeneratorExpression (or ClassExpression)  you need 
> to parenthesize to force it to be an expression rather than a 
> declaration, such as:
> export default (function fact(n) {...});

Right-o, this is what jogged my memory, first sentence above.

/be


More information about the es-discuss mailing list