function f() { return s; } // works fine though no `s` is defined yet !
var s=1;
f(); // 1
delete s;
var s=2;
f(); // 2
(function() {
var s=3;
f(); // 2 and not 3 which means lexical scoping is at play (?)
})();
first off, you can close over a variable (s
) which is not yet defined. How is that possible if lexical (static) scoping is used?
second, after deleting the original s
, f()
can find the new s
. Does this mean closures are bound to variable names rather than references or symbol table indexes or something more machine-level? I would expect from e lexical scoping closure to throw an error since the original s
is deleted. The new s
just reuses the name and has nothing to do with the original s
.
And third, the s
inside the anonymous function scope is not used by f()
, does this mean indeed lexical scoping is at play?