Ambiguity with default exports and live bindings?
Logan Smyth
loganfsmyth at gmail.com
Wed Jul 6 23:22:38 UTC 2016
>From the code perspective, changing the value of `A` will not update the
value of the export, but it's not quite that it's not live, it's that
you're not exporting what you think you're exporting.
```
export default A;
```
is essentially short for
```
const fakeInaccessibleVariable = A;
export {fakeInaccessibleVariable as default};
```
Changing the value of the `A` binding at a later time has no effect on the
exported value because it won't change the value if the inaccessible
variable.
In the spec this variable is referred to as `*default*` (note the `*`s in
the name mean it's not possible to have a program with that as a variable
name, making collisions with a variable impossible). This is because
`export default A;` falls into the same category as `export default 123;`,
where you are exporting an arbitrary value as the default. In the case of
`export default A;` you are exporting the value of `A` at the time that the
`export default` expression is evaluated. In the spec this is the format
`export default [lookahead ∉ { function, class }]
AssignmentExpression[In];` You can see this syntax construct's behavior
defined in the last block in
http://www.ecma-international.org/ecma-262/7.0/#sec-exports-runtime-semantics-evaluation
.
On Wed, Jul 6, 2016 at 3:02 PM, /#!/JoePea <joe at trusktr.io> wrote:
> Is it true one of the following does not create a live binding?
>
> ```js
> let A = 123
> export default A // not a live binding?
> ```
>
> compared to
>
> ```js
> let A = 123
> export {A as default} // live binding?
> ```
>
> If so, this seems like large source for unexpected behavior when people
> create modules. I can imagine people easily overlooking the difference and
> expecting live bindings in both cases (this already happened to me).
>
>
> */#!/*JoePea
>
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20160706/eea20561/attachment.html>
More information about the es-discuss
mailing list