本文介绍了[].forEach.call(...?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我非常喜欢在 nodeList 上使用 forEach 方法,如下所示:

I'm a big fan of using the forEach method on nodeLists like this:

var nodes = document.querySelectorAll(".foo");

[].forEach.call(nodes, function (item) {
    //do stuff with item
});

不过我想知道,这样做会比常规方式花费更长的时间吗?例如

I was wondering though, does doing it that way take longer than the regular way?e.g.

for(var i=0;i<nodes.length;i++){
    //do stuff with nodes[i];
}

推荐答案

这里有一个 不错的性能比较.根据它 Array.forEach 比本地 for 循环慢.

Here's a nice performance comparison. According to it Array.forEach is slower than a native for loop.

这篇关于[].forEach.call(...?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:19
查看更多