admin管理员组文章数量:1178539
I have seen anonymous functions inside for loops to induce new scope on the web in one or two places and would like to know if it makes sense.
for example:
var attr, colors = ['green','blue','red'];
for ( attr = 0; attr < colors.length; attr++) {
(function() {
var colorAttr = colors[attr];
// do something with colorAttr
})();
}
I understand it has something to do with keeping the scope inside the for loop clean, but in what situations would this be necessary? Would it be good practice to do this everywhere you need to declare a new var inside the for loop?
I have seen anonymous functions inside for loops to induce new scope on the web in one or two places and would like to know if it makes sense.
for example:
var attr, colors = ['green','blue','red'];
for ( attr = 0; attr < colors.length; attr++) {
(function() {
var colorAttr = colors[attr];
// do something with colorAttr
})();
}
I understand it has something to do with keeping the scope inside the for loop clean, but in what situations would this be necessary? Would it be good practice to do this everywhere you need to declare a new var inside the for loop?
Share Improve this question asked Dec 20, 2012 at 17:02 EvanEvan 6,1159 gold badges35 silver badges64 bronze badges 7 | Show 2 more comments2 Answers
Reset to default 392021 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.
You're almost there. It makes only sense in your snippet, if you pass in the attr
value into your self invoking function as an argument. That way, it can store that variable within its very own scope object
(function( attr ) {
var colorAttr = colors[attr];
// do something with colorAttr
})( attr );
Now, the activation object respectively lexical environment record (which are ES3 and ES5 scope objects) will have an entry for whatever value is behind attr
and therefore, its closured.
本文标签: using anonymous function in javascript for loopsStack Overflow
版权声明:本文标题:using anonymous function in javascript for loops - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738021058a2050536.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
attr
changes as the loop progresses, so if you usecolors[attr]
in a callback function, it won't refer to thecolors[attr]
that you actually want. – Blender Commented Dec 20, 2012 at 17:05colors[]
changes. – mkey Commented Dec 20, 2012 at 17:07colorAttr
is scoped to that immediately executing function, but it can walk up to the parent scope and happily accessattr
. – Fenton Commented Dec 20, 2012 at 17:09