The with
statement doesn't creates a full new lexical scope, it just introduces an object in front of the scope chain, for example, if you capture the i
variable, it will work:
for (var i = 1; i <= 5; i++) {
with({x:i}) {
document.getElementById('link' + i).onclick = function() {
alert(x);
return false;
};
}
}
Let me try to explain it better with another example:
var x = 10, y = 10; // Step 1
with ({x: 20}) { // Step 2
var x = 30, y = 30; // Step 3
alert(x); // 30
alert(y); // 30
}
alert(x); // 10
alert(y); // 30
In the Step 1, the x
and y
variables 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 the with
statement, now the scope chain looks something like this:
________ ________
| x = 10 | <--------- | x = 20 |
| y = 10 | ¯¯¯¯¯¯¯¯¯
¯¯¯¯¯¯¯¯
In the Step 3, another var
statement is executed, but it has no effect because as I said before, only functions create a full lexical scope.
The var
statement has no effect, but the assignment has, so when the x
variable is resolved, is reached on the first object on the scope chain, the one we introduced using with
.
The y
identifier 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:
________ ________
| x = 10 | <--------- | x = 30 |
| y = 30 | ¯¯¯¯¯¯¯¯¯
¯¯¯¯¯¯¯¯
When the with
statement ends, the scope chain is finally restored:
________
| x = 10 |
| y = 30 |
¯¯¯¯¯¯¯¯
Edit:
Let me expand a little bit and talk about functions.
When a function is created its current parent scope is bound, for example:
var fn;
// augment scope chain
with ({foo: "bar"}) {
fn = function () { // create function
return foo;
};
}
// restored scope chain
fn(); // "bar", foo is still accessible inside fn
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 var
and functions declared with the function
statement, 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:
var x = 10, y = 10; // Step 1
(function () { // Step 2
var x, y;
x = 30; // Step 4
y = 30;
alert(x); // 30
alert(y); // 30
})(); // Step 3
alert(x); // 10 // Step 5
alert(y); // 10
In the Step 1, again as in my first example, the x
and y
variables 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 x
and y
.
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 x
and y
variables declared inside this new function, the scope chain at this moment looks like this:
parent scope Variable Object
________ _______________
| x = 10 | <--------- | x = undefined |
| y = 10 | | y = undefined |
¯¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
Then in the Step 4, the assignment of x
and y
is done, but since the new lexical scope has been created, it doesn't affect the outer values.
parent scope Variable Object
________ ________
| x = 10 | <--------- | x = 30 |
| y = 10 | | y = 30 |
¯¯¯¯¯¯¯¯ ¯¯¯¯¯¯¯¯
And finally, in the Step 5, the function ends and the scope chain is restored to its original state.
________
| x = 10 |
| y = 10 |
¯¯¯¯¯¯¯¯
Recommended lectures: