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

Glen Huang curvedmark at gmail.com
Mon Dec 15 08:16:02 PST 2014


Thank you for the detailed explanation. Now I have a better understand of the export declaration.

>  I would have no idea what `export default var a = 1;` was supposed to mean.

Looking back, this does seem unclear, but indulge me to share how I come up with the proposed syntax:

I was trying to export a point data structure for the mouse position. This was my initial try:

export var x = 0;
export var y = 0;
document.addEventListener(“mousemove”, (ev) => {clientX: x, clientY: y} = ev);

But then I realized in order to consume it, i need to do “import * as mousePosition from …”. This doesn’t look right to me, because the exported obj isn't an aggregation of independent things (the "* as …" syntax looks like a reminder that properties are independent), I want to use it like “import mousePosition from …”.

So giving the experience of you can merge export and a variable declaration into a single line, I tried to do:

export default var pt = { x: 0, y: 0 };
document.addEventListener(“mousemove”, (ev) => {clientX: pt.x, clientY: pt.y} = ev);
// hope i got the destructuring assignment right

Thus this proposal.

So obviously “merging” concept doesn’t apply when default is involved, and I now know why. Thanks again.

> On Dec 15, 2014, at 11:31 PM, Dave Herman <dherman at mozilla.com> wrote:
> 
> On Fri, Dec 12, 2014 at 8:19 PM, Glen Huang <curvedmark at gmail.com <mailto:curvedmark at gmail.com>> wrote:
> You can already do "var a = 1;export default a;”. Why not make "export default var a = 1;” valid?
> 
> Because the former is creating an exported variable called 'default' and assigning its initial value to the result of evaluating an expression that happens to evaluate the current value of 'a'. There's nothing special about the fact that you used 'a' there, it's just an ordinary expression that happens to evaluate a variable.
> 
> (For historical interest, this was why I was in favor of using the equals sign in the syntax, to make it clear that export default is doing an assignment of an initializer expression to a variable, e.g.:
> 
>   export default = a;
> 
> But this was unpopular and I didn't push the issue.)
> 
> At a more basic level, from a "principle of least surprise" perspective, I would have no idea what `export default var a = 1;` was supposed to mean.
> 
> Dave

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20141216/a8ce9b78/attachment.html>


More information about the es-discuss mailing list