JavaScript的 - myArray.forEach的细微之处,相较于循环 [英] JavaScript - Nuances of myArray.forEach vs for loop

查看:152
本文介绍了JavaScript的 - myArray.forEach的细微之处,相较于循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多类似的建议使用的问题:

I've seen plenty of questions that suggest using:

for (var i = 0; i < myArray.length; i++){ /* ... */ }

而不是:

for (var i in myArray){ /* ... */ }

有关阵列,由于不一致的迭代(见这里)。

for arrays, due to inconsistent iteration (see here).

不过,我似乎无法找到任何东西,这似乎preFER面向对象循环:

However, I can't seem to find anything that seems to prefer the object oriented loop:

myArray.forEach(function(item, index){ /* ... */ });

哪种方式更直观,似乎给我。

Which seems way more intuitive to me.

有关我的当前项目,IE8相容性是很重要的,而且我在考虑使用<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach#Polyfill\">Mozilla's填充工具,但我不是100%肯定这将如何工作。

For my current project, IE8 compatibilty is important, and I'm considering using Mozilla's polyfill, however I'm not 100% sure how this will work.


  • 是否有通过现代浏览器for循环标准(上述第一个例子)和Array.prototype.forEach实施有何区别?

  • 是否有现代化的浏览器实现和Mozilla的实现链接到上述任何区别(特别是关于IE8)?

  • 性能是没有那么多的问题,仅仅一致性与性能遍历的。

推荐答案

环路之间最实质性的区别的forEach 方法是,同前,你可能圈外。您可以模拟继续通过简单地传递到的forEach 函数返回,但也没有办法完全停止循环。

The most substantive difference between the for loop and the forEach method is that, with the former, you may break out of the loop. You can simulate continue by simply returning from the function passed to forEach, but there is no way to stop looping altogether.

除此之外,在两个有效地完成相同的功能。另一个微小的差别涉及指数(包含所有变量和)在for循环的范围,因变量吊装。

Aside from that, the two accomplish effectively the same functionality. Another minor difference involves the scope of the index (and all containing variables) in the for loop, due to variable hoisting.

// 'i' is scoped to the containing function
for (var i = 0; i < arr.length; i++) { ... }

// 'i' is scoped to the internal function
arr.forEach(function (el, i) { ... });

不过,我发现,的forEach 是更前pressive,它重新presents你的意图通过数组的每个元素进行迭代,并提供了对元件,而不仅仅是索引的参考。总体而言,它主要涉及到个人的口味,但如果你可以使用的forEach ,我会建议使用它。

However, I find that forEach is much more expressive—it represents your intent to iterate through each element of an array, and it provides you with a reference to the element, not just the index. Overall, it mostly comes down to personal taste, but if you can use forEach, I would recommend using it.

有两个版本之间多了一些重大分歧,特别是关于性能。事实上,简单的for循环的性能比的forEach 方法相当好,通过的这个jsperf测试

There are a few more substantial differences between the two versions, specifically regarding performance. In fact, the simple for loop performs considerably better than the forEach method, as demonstrated by this jsperf test.

不管是不是这样的表现是必要的,你是由你来决定,并且在大多数情况下,我赞成超速前pressiveness。这个速度差可能是由于基本循环和稀疏阵列操作时,方法之间的细微语义差异,如在该答案<指出/ A>

Whether or not such performance is necessary for you is up to you to decide, and in most cases, I would favor expressiveness over speed. This speed difference is likely due to the minor semantic differences between the basic loop and the method when operating on sparse arrays, as noted in this answer.

如果您不需要的行为的forEach 和/或你需要提前跳出循环,可以用罗Dash的的 _。每个 作为替代,这也将工作跨浏览器。如果你正在使用jQuery,它也提供了类似的 $。每个 ,只要注意传递到回调函数中的每个参数变化的差异。

If you don't need the behavior of forEach and/or you need to break out of the loop early, you can use Lo-Dash's _.each as an alternative, which will also work cross-browser. If you're using jQuery, it also provides a similar $.each, just note the differences in arguments passed to the callback function in each variation.

(至于的forEach 填充工具,它应该在旧的浏览器是没有问题的,如果你选择走这条路。)

(As for the forEach polyfill, it should work in older browsers without problems, if you choose to go that route.)

这篇关于JavaScript的 - myArray.forEach的细微之处,相较于循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆