If the with statement in Javascript creates a new scope, shouldn’t clicking on the links show a different x which are in different scopes? It doesn’t.
<a href="#" id="link1">ha link 1</a>
<a href="#" id="link2">ha link 2</a>
<a href="#" id="link3">ha link 3</a>
<a href="#" id="link4">ha link 4</a>
<a href="#" id="link5">ha link 5</a>
<script type="text/javascript">
for (i = 1; i <= 5; i++) {
with({foo:"bar"}) {
var x = i;
document.getElementById('link' + i).onclick = function() { alert(x); return false; }
}
}
</script>
The
withstatement doesn’t creates a full new lexical scope, it just introduces an object in front of the scope chain, for example, if you capture theivariable, it will work:Let me try to explain it better with another example:
In the Step 1, the
xandyvariables are declared and they are part of the first object in the scope chain, the global object.In the Step 2, a new object (
{x:20}) is introduced into the scope chain by thewithstatement, now the scope chain looks something like this:In the Step 3, another
varstatement is executed, but it has no effect because as I said before, only functions create a full lexical scope.The
varstatement has no effect, but the assignment has, so when thexvariable is resolved, is reached on the first object on the scope chain, the one we introduced usingwith.The
yidentifier is resolved also, but it is not found on the first object in the chain, so the lookup continues up, and finds it on the last object, the scope chain after the assignments looks like this:When the
withstatement ends, the scope chain is finally restored:Edit:
Let me expand a little bit and talk about functions.
When a function is created its current parent scope is bound, for example:
A new lexical scope is created and added to the scope chain when the function is executed.
Basically all identifiers (names) of function arguments, variables declared with
varand functions declared with thefunctionstatement, are bound as properties of a new object created behind the scenes, just before the function itself executes (when controls enters this new execution context).This object is not accessible through code, is called the Variable Object, for example:
In the Step 1, again as in my first example, the
xandyvariables are declared and they are part of the first object in the scope chain, the global object.In the Step 2, a new function object is created, the parent scope is stored in this moment, into the [[Scope]] of that function, containing now
xandy.In the Step 3, the function is invoked, starting the Variable Instantiation process, which creates a new object in the scope chain, containing the locally scoped
xandyvariables declared inside this new function, the scope chain at this moment looks like this:Then in the Step 4, the assignment of
xandyis done, but since the new lexical scope has been created, it doesn’t affect the outer values.And finally, in the Step 5, the function ends and the scope chain is restored to its original state.
Recommended lectures: