2021 Update
var
used to be the only way to declare a variable. But we now have const
and let
which solve this problem in a better way. These variable declarations do respect the loop as a scope to bind to, which means the following snippet works fine and there is no need for an anonymous function to capture those values.
const colors = ['green', 'blue', 'red'];
for (let i = 0; i < colors.length; i++) {
const color = colors[i];
setTimeout(function() {
alert(color);
}, i * 1000);
}
What follows below is my original answer to this question from 2012.
When you have inner functions that are not executed immediately, as part of the loop.
var i, colors = ['green', 'blue', 'red'];
for (i = 0; i < colors.length; i++) {
var color = colors[i];
setTimeout(function() {
alert(color);
}, i * 1000);
}
// red
// red
// red
Even though var color
is inside the loop, loops have no scope. You actually only have one variable that every loop iteration uses. So when the timeouts fire, they all use the same value, the last value set by the loop.
var i, colors = ['green', 'blue', 'red'];
for (i = 0; i < colors.length; i++) {
(function(color) {
setTimeout(function() {
alert(color);
}, i * 1000);
})(colors[i]);
}
// green
// blue
// red
This one captures the value at each iteration into an argument to a function, which does create a scope. Now each function gets it's own version of a color
variable which won't change when functions created within that loop are later executed.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…