Take let variable out of temporal dead zone

Isiah Meadows isiahmeadows at gmail.com
Fri Apr 15 18:00:47 UTC 2016


Try `window.foo`. That's usually what's done within global IIFEs, which has
similar scope restrictions.

On Fri, Apr 15, 2016, 13:52 Oriol Bugzilla <oriol-bugzilla at hotmail.com>
wrote:

> Consider this code:
> ```html
> <script>
> let {foo} = null; // TypeError
> </script>
> <script>
> // Here I want to assign some some value to foo
> </script>
> ```
>
> The first script attempts to let-declare `foo` via a destructuring
> assignment. However, `null` can't be destructured, so the assignment throws
> a TypeError.
> Some alternatives which would lead to the same problem are `let foo =
> null.throw` and `let foo = (() => {throw;})()`.
>
> Then the `foo` variable is declared but uninitialized, so if in the 2nd
> script I attempt to reference `foo`, it throws:
> ```js
> foo = 123; // ReferenceError: can't access lexical declaration `foo'
> before initialization
> ```
>
> And `let` variables can't be redeclared:
> ```js
> let foo = 123; // SyntaxError: redeclaration of let foo
> ```
>
> Is this behaviour intended? Is there any way to take `foo` out of the TDZ,
> so that I can assign values and read them?
>
> - Oriol
> _______________________________________________
> 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/20160415/ca265647/attachment.html>


More information about the es-discuss mailing list