return when desugaring to closures

Brendan Eich brendan at mozilla.com
Sat Oct 11 14:42:36 PDT 2008


On Oct 11, 2008, at 12:55 PM, David Herman wrote:

>> How to define a variable that is local to the enclosing lambda? Isn't
>> the ability to do that essential?
>
> No. With all due respect to Brendan, `var' hoisting to the top of a  
> function body is one of the more problematic aspects of ES's  
> semantics.

I agree, it's no skin off my nose -- 'var' hoisting was an artifact of  
function implementation in Netscape 2, and did not apply to global  
vars then. It was standardized as hoisting in all kinds of code  
(global, function, and eval). We are stuck with it. However, hoisting  
still applies to let:


> If you want a local variable, use `let' -- it'll be local to its  
> containing block. If you want a variable that is local to the entire  
> body of a `lambda', use `let' at the top level of the `lambda' body.

While let is local to containing block, the let-as-new-var proposal  
(implemented in Firefox 2 and up) hoists to top of block. So you  
cannot initialize the inner x using the outer x's value:

   {
     let x = 42;
     {
       let x = x; // undefined, not 42.
       alert(x);
     }
     alert(x); // 42, of course
   }

We've discussed making use-before-set a strict error, but we've  
avoided it. The initialiser is not mandatory, and we do not wish to  
impose costly analysis on small implementations.

/be


More information about the Es-discuss mailing list