I think, but I cannot confirm through the ecmascript specification that the string "var x = 2" is checked during the definition of the function, and the local region "x" is applied to the entire function, and not just after "var x" in the function.
Here's a simpler test case (tested with jsdb ):
js>x = 0; 0 js>function foo(k) { x=k; } js>function bar(k) { var x=k; } js>function baz(k) { x=k; var x=2; } js>foo(1) js>x 1 js>bar(2) js>x 1 js>baz(3) js>x 1
You will notice that the only function that affects the global "x" is foo() . The bar() function obviously only affects local x. But the baz() function only affects local x. You might think that assignment x=k; will affect global x, as it happens "before" the next statement var x = 2; , which explicitly affects the local variable x. But I am sure that if you execute var x in a function, the interpreter sees it and applies it to all applications of x in this area. The same situation as yours.
Jason s
source share