Confusion about different [[Scope]] between Function Declaration and Function Expression

Brendan Eich brendan at mozilla.com
Thu Dec 13 10:07:52 PST 2012


Allen Wirfs-Brock wrote:
>   Some implementations bind the function name scoped to the block others bind the name scoped to the enclosing function.

Detailed aside: no standard implementation actually binds to a block 
scope (yet -- this is proposed for ES6 and implementations are 
appearing, e.g. under a flag in V8 in Chrome).

Rather, the implementations bind the function "sub-statement" by its 
name in the outer function's activation but either always, as if hoisted 
independent of control flow (last in source order among several for the 
same name wins), or (SpiderMonkey in Firefox, Rhino, possibly others) 
binding the name as if by assignment if and only if control flow reaches 
the nested function declaration.

Web content relies on the intersection semantics. E.g.,

   function outer() {
     ...
     if (cond) {
       function inner() { ... }
       obj.callback = inner;
     }
     ...
   }

Fortunately, ES6's block-scoped semantics for functions declared in 
blocks is in this intersection.

/be


More information about the es-discuss mailing list