Function declarations in statements

Brendan Eich brendan at mozilla.org
Sat Mar 17 11:27:38 PDT 2007


On Mar 17, 2007, at 4:41 AM, Igor Bukanov wrote:

> js> var x = 10;
> js> function f() { print(x); if (false) var x; }
> js> f();
> undefined
> js>  function g() { print(f); if (false) function f() {}; }
> js> g()
> function f() {
>     print(x);
>     if (false) {
>         var x;
>     }
> }
>
> That is, in the first f() "if (false) var x;" defines x for the whole
> function yet in g() the function statement does not bind the local
> name until executed.

That's right -- var and function are not equivalent. You cannot  
rewrite function f() {} as var f = function (){}.

Independent of this long-standing fact, which is detectable even in  
ES1-3, never mind function statements or other such extensions: the  
idea with function statements in SpiderMonkey (and Rhino, IIRC) is  
precisely that they allow conditional binding of DontDelete DontEnum  
names to function objects, depending on control flow.

/be




More information about the Es4-discuss mailing list