JavaScript for 循环替代:repeat(n, function(i) { ... });
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4929113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript for-loop alternative: repeat(n, function(i) { ... });
提问by ?ime Vidas
This is the regular for-loop:
这是常规的 for 循环:
for (var i = 0; i < n; i++) { ... }
It is used to iterate over arrays, but also to just repeat some process ntimes.
它用于迭代数组,但也用于重复一些处理n时间。
I use the above mentioned form, but it repulses me. The header var i = 0; i < n; i++is plain ugly and has to be rewritten literally every time it is used.
我使用上述形式,但它排斥我。标题var i = 0; i < n; i++非常丑陋,每次使用时都必须从字面上重写。
I am writing this question because I came up with an alternative:
我写这个问题是因为我想出了一个替代方案:
repeat(n, function(i) { ... });
Here we use the repeatfunction which takes two arguments:
1. the number of iterations,
2. a function which body represents the process that is being repeated.
这里我们使用
带有repeat两个参数的函数:
1. 迭代次数,
2. 一个函数,它的主体代表正在重复的过程。
The "code-behind" would be like so:
“代码隐藏”将是这样的:
function repeat(n, f) {
for (var i = 0; i < n; i++) {
f(i);
}
}
(I am aware of the performance implications of having two additional "levels" in the scope chain of the process)
(我知道在流程的范围链中有两个额外的“级别”对性能的影响)
BTW, for those of you who use the jQuery library, the above mentioned functionality can be achieved out-of-the-box via the $.eachmethod like so:
顺便说一句,对于那些使用 jQuery 库的人来说,上面提到的功能可以通过$.each像这样的方法开箱即用地实现:
$.each(Array(n), function(i) { ... });
So what do you think? Is this repeatfunction a valid alternative to the native for loop? What are the down-sides of this alternative (other than performance - I know about that)?
所以你怎么看?这个repeat函数是原生 for 循环的有效替代吗?这种替代方案的缺点是什么(除了性能 - 我知道)?
Native:
本国的:
for (var i = 0; i < 10; i++) {
// do stuff
}
Alternative:
选择:
repeat(10, function(i) {
// do stuff
});
采纳答案by Jeff
it's an interesting thought, but if you dislike the syntax for the loop, you could always do a different type of loop:
这是一个有趣的想法,但如果你不喜欢循环的语法,你总是可以做不同类型的循环:
var i = arr.length;
while (i--) {
// do stuff
}
the reverse while loop is generally faster than a for loop as well.
反向 while 循环通常也比 for 循环快。
回答by Gonzalo Larralde
You say you want a revolution... Well, you know: ruby did it just before (?)
你说你想要一场革命......好吧,你知道:ruby就在之前(?)
Number.prototype.times = function(func) {
for(var i = 0; i < Number(this); i++) {
func(i);
}
}
means
方法
(50).times(function(i) {
console.log(i)
})
Anyway, don't fight against C, you'll always lose :-P
无论如何,不要与C对抗,你永远都会输:-P
回答by David Tang
To address the issue of not having the break statement as others have mentioned, I would solve it this way:
为了解决没有其他人提到的 break 语句的问题,我会这样解决:
function repeat(n, f) {
for (var i = 0; i < n; i++) {
if (f(i) === false) return;
}
}
Then returning falsefrom within a loop handler will be equivalent to break.
然后false从循环处理程序中返回将等效于break.
Another disadvantage is that the context changes. You may want to add the option of proxying a context into the loop handlers:
另一个缺点是上下文会发生变化。您可能希望将代理上下文的选项添加到循环处理程序中:
function repeat(context, n, f) {
if (!f) f = n, f = context, context = window;
for (var i = 0; i < n; i++) {
if (f.call(context, i) === false) return;
}
}
Now, an advantageis that the index is preserved by the function scope, to avoid a common bug:
现在,一个优点是索引由函数作用域保留,以避免常见错误:
for (var i = 0; i < 10; i++) {
setTimeout(function () {
alert(i); // Will alert "10" every time
}, 1000);
}
repeat(10, function (i) {
setTimeout(function() {
alert(i); // Will alert "0", "1", "2", ...
}, 1000);
});
回答by JCOC611
It seems pretty valid. I honestly don't think that performance would decrease too much. But there is however one big downside, that is easily fixable: the breakstatement.
这似乎很有效。老实说,我认为性能不会下降太多。但是有一个很大的缺点,很容易修复:break语句。
function repeat(n, f) {
for (var i = 0; i < n; i++) {
var tcall=i;
tcall.die=function(){i=n}
f.call(tcall);
}
}
This way you would be able to call this.die()instead of break;which I think would throw an error.
这样你就可以调用this.die()而不是break;我认为会抛出错误的方法。
回答by Gunnar Hoffman
Besides what you have already stated the main downside I see is that a "return" statement will work differently. (Which is often why I end up using "for" over "$.each" many times in my own ventures.)
除了你已经说过的,我看到的主要缺点是“返回”语句的工作方式不同。(这通常是我在自己的冒险中多次使用“for”而不是“$.each”的原因。)

