Closures and let blocks

Brendan Eich brendan at mozilla.org
Thu Jan 3 09:22:46 PST 2008


On Jan 3, 2008, at 8:29 AM, Igor Bukanov wrote:

> For functions defined with ES4 let block, does the scope of the
> closure includes the let block?

The let block head scopes initialisers in the head to the outer  
scope, but that has nothing to do with named function expression  
scoping (not the same as function definition -- you wrote "functions  
defined").

> I.e will f in the following examples refer to the function itself or
> to the the name from the outer scope?
>
> let (f = function() { ...  f() ... }) { ... }

The call to f within itself always refers to itself. This is defined  
by ES3, although ES4 changes the binding mechanism to avoid scope  
object escape and Object hijacking attacks. See the compatibility  
guide at http://www.ecmascript.org/es4/spec/incompatibilities.pdf  
section 1.6.

> I assume that f should refer to f from the outer scope to allow a  
> usage like:
>
> let (function f() { print("f is called"); f(); }) {

This is not valid syntax. The let head should contain optionally  
annotated, optionally initialised, unqualified variable names, comma- 
separated. A function expression as initialiser is just an  
expression. If it has a name, it's a named function expression per  
ES3 chapter 13, third semantic definition.

/be



More information about the Es4-discuss mailing list