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

Dave Herman dherman at mozilla.com
Mon Dec 15 08:25:48 PST 2014


On Mon, Dec 15, 2014 at 8:16 AM, Glen Huang <curvedmark at gmail.com> wrote:

>
>
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.
>

I can see that, and thanks for the field report! Basically the summary is,
any time you want a default export object (as opposed to a function or
class, where you have `export default class` and `export default function`
shorthands) that you also want to refer to internally in the module that
defines it, you'll end up needing a two-liner:

```js
var o = { ... };
export default o;
```

If you only need the properties of the object, not the object itself, one
style you can use is to name all the property values with local variables
but not the object itself:

```js
function foo(...) { ... }
function bar(...) { ... }
var baz = ...;
...
export default { foo, bar, baz };
```

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

No worries. JFTR, as I alluded to above, if you're exporting a function or
class, you *do* get the merging:

```js
export default class Foo {
  ...
}
var x = new Foo(...);
...
```

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


More information about the es-discuss mailing list