restrictions on let declarations

Dean Landolt dean at deanlandolt.com
Thu Jan 30 08:07:29 PST 2014


On Thu, Jan 30, 2014 at 10:59 AM, John Barton <johnjbarton at google.com>wrote:

>
>
>
> On Thu, Jan 30, 2014 at 7:54 AM, Brendan Eich <brendan at mozilla.com> wrote:
>
>> John Lenz wrote:
>>
>>> Generally, I've always thought of:
>>>
>>> "if (x) ..." as equivalent to "if (x) { ... }"
>>>
>>
>> let and const (and class) are block-scoped. {...} in your "if (x) {...}"
>> is a block. An unbraced consequent is not a block, and you can't have a
>> "conditional let binding".
>>
>> The restriction avoids nonsense such as
>>
>> let x = 0; { if (y) let x = 42; alert(x); }
>>
>> What pray tell is going on here, in your model?
>
>
> I'm with John: the alert should say 0 and I can't see why that is not
> obvious.
>


It's not obvious at all -- what happens when you drop the initial `let x =
0;` and you just have `{ if (y) let x = 42; alert(x); }` -- now what
happens? Is x declared or not?

To my mind `if (y) let x = 42;` reads like it's own 1-line noop block -- at
least, that's what I'd expect of the scope. So while it could be allowed in
that sense, it'd only serve as a footgun when y is true.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140130/bd9d2087/attachment.html>


More information about the es-discuss mailing list