Conflicting declarations -- Was: Re: local

Brendan Eich brendan at mozilla.org
Fri Aug 22 07:51:40 PDT 2008


On Aug 22, 2008, at 7:26 AM, Ingvar von Schoultz wrote:

> Brendan Eich wrote:
>> More helpful would be comments on the utility of let blocks  
>> (a.k.a. let statements) and let expressions.
>
> While reading up on this, I noticed that you can declare the
> same name with both let and var in the same scope:
>
>     var x = 'global';
>     let x = 42;

If these are both within an explicit block, the let will shadow the  
hoisted var.


> Even if you do this in the global scope or function top level,
> both will exist, in the same scope, one shadowing the other
> (but details differ between texts and test results).

This past April at the ES4 WG meeting we decided to make let equate  
to var at top level within a function body or program. This is *not*  
what JS in Firefox 2 and 3 do in functions, but it is what the  
SpiderMonkey implementation does at top of program. We are fixing  
things to match the latest proposal all around.

Another thing we do that's a bit harsh is to call repeated let x;  
declarations in the same explicit block an error.


> In my opinion this is not a useful feature, quite the contrary.
> If the variables are far apart on the page, the disappearance of
> one of them will be mysterious and difficult to debug.

It's also a pain to implement in few passes at top level, if one  
prefers to parse a top-level statement at a time and recycle the AST  
after generating code.


> In the special case of global and function-top-level scope,
> intuitively let and var seem synonymous.

Agreed, and that's what Firefox 3.1 will do.


> But throwing an error seems fine too.
>
>     {   let x;
>         {   var x = 10;
>         }
>     }
>
> The let hides the var. This should throw an error too, as I see
> it. If a name can't be reached where it's declared, that's hard
> to debug.

Could be -- thanks for the feedback.

/be



More information about the Es-discuss mailing list