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

Allen Wirfs-Brock allen at wirfs-brock.com
Mon Dec 15 21:52:37 PST 2014


On Dec 15, 2014, at 8:50 PM, Brendan Eich wrote:

> Kevin Smith wrote:
>> 
>>    ```js
>>    export default function a() {}
>>    a = 2;
>>    ``` 
>>    (This should be 2, right?)
>> 
>> 
>> I *think* the "default" binding in this case would still point to the function.  I find this particular example completely baffling, to be honest.
> 
> 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. 

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.

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) {...});

Regarding Kevin's a function:

export default function a() {}; // bound name:'a' (a mutable minding), hosted initial value: function a() )), export name and association 'default'->'a'
... // any call out to a module that references the 'default' export of this module will see the value function a() {}
a=2;  //changes the value of the multiple binding 'a', export name association is still 'default'->'a'
... // any call out to a module that referenes the 'default' export of this module will see the value 2

Finally, it's import that all exports (including default) are always an association between an export name and a module local binding, never between a export name and a specific value.

Allen
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141215/17fe9d2e/attachment-0001.html>


More information about the es-discuss mailing list