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

Glen Huang curvedmark at gmail.com
Mon Dec 15 19:00:38 PST 2014


On a second thought, I do have another question:

```js
export var a = 1;
a++;
```
I believe the exported value is 2?

```js
var a = 1;
export default a;
a++;
```
And this is 1?

```js
var a = 1;
export { a };
a++;
```
(Not sure about this one, looks like 1)

So the question is, there is no way to bind variables with the “export default” syntax, when it is used to export an object?

```js
export default function a() {}
a = 2;
```
(This should be 2, right?)

> On Dec 16, 2014, at 12:25 AM, Dave Herman <dherman at mozilla.com> wrote:
> 
> On Mon, Dec 15, 2014 at 8:16 AM, Glen Huang <curvedmark at gmail.com <mailto: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/20141216/6ad97318/attachment.html>


More information about the es-discuss mailing list