Function declarations in statements

Igor Bukanov igor at mir2.org
Sat Mar 17 04:41:23 PDT 2007


On 17/03/07, Brendan Eich <brendan at mozilla.org> wrote:
> The IE JScript bug should be fixed. It's just a deviation from the
> standard, as far as I can tell.

But then should SpiderMonkey aslo be fixed? Consider the following
shell session:

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.

For me JScript behavior is in fact more consistent. There both var and
function statement is executed on the function entrance no matter
where they are placed. Yet in SpiderMonkey only var has such property
while function having an extra weiredness:

js>  var x = 10;
js> function f() { print(x); function x() {} }
js> f()
function x() {
}
js> function g() { print(x); if (true) function x() {} }
js> g()
10

Now try to explain to a new guy the logic behind this.

Regards, Igor



More information about the Es4-discuss mailing list