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

Glen Huang curvedmark at gmail.com
Mon Dec 15 21:40:03 PST 2014


@Kevin @Brendan

Thanks for the detailed explanation. If I’m not wrong, the exported binding is the module record in the spec, right?

Also sorry for the confusion about the “object” concept I mentioned. By “object”, I mean anything qualifies as an "AssignmentExpression” in the spec.

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 };"

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?

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;
```

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


> On Dec 16, 2014, at 12:50 PM, Brendan Eich <brendan at mozilla.org> 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.
> 
> /be



More information about the es-discuss mailing list