Redefining a let variable inside a for loop scope doesn't work?

J Decker d3ck0r at gmail.com
Fri Jul 15 16:31:47 UTC 2016


On Fri, Jul 15, 2016 at 7:18 AM, Allen Wirfs-Brock <allen at wirfs-brock.com>
wrote:

> This is by design.  ECMAScript block scoped declarations generally conform
> to the principle that within a single block or statement a name may have
> only a single binding. For example,
>
> ```js
> let x=0;
> {
>   let x = x+1;  //access to uninitialized variable error because both the
> RHS and LHS refer to the inner x
>                      //which has not yet been initialized when the RHS is
> evaluated
> }
>
> ```
>
> The same principle also applies to
> ```js
> let n = {a:[]};
> for (let n of n.a) ;
> ```
>
> although the actual scoping of the `for` statement is more complex.
> Basically, such `for` statements consider all names bound by the `let` part
> of of the `for` header to be undefined while the `of` expression is being
> evaluated.
>
>
Wouldn't that scope for the 'for' loop declaration allow testing the 'n'
variable after the loop?  (test to see if it was broken out of completed),
without requiring extra braces around the for?  (Although sounds like just
dong

{
   for( let n ...) {
   }
   // can test/use N here?
}

would also have worked like the workaround for C#/C++?


> This is done to avoid any confusion about which `n` the expression
> references. Such confusion is avoid by all references to the `let` bound
> names from the RHS errors.
>
> Allen
>
>
>
> _______________________________________________
> 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/20160715/884690c2/attachment.html>


More information about the es-discuss mailing list