forEach和for循环之间的区别 [英] Difference between forEach and for loop in javascript

查看:200
本文介绍了forEach和for循环之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道:JavaScript中 forEach 和循环之间是否有显着的区别。



考虑这个例子:

  var myArray = [1,2,3,4 ]。 

myArray.forEach(function(value){
console.log(value);
});
$ b $ for(var i = 0; i< myArray.length; i ++){
console.log(myArray [i]);

$ / code>

这是我研究的一部分:


  1. 效果:根据 JsPerf : forEach比for循环稍慢。

  2. 可用性:在forEach循环的情况下,我们无法从回调中分解/返回。

例如:您想知道一个数字是否为素数。我认为使用for循环比使用forEach循环要容易得多。


  1. 可读性:使用for循环使代码比在代码中具有forEach更具可读性。

  2. 浏览器兼容性:forEach is not supported in IE< 9我们的代码中引入了一些填充。

我的问题是:


  1. forEach for循环的优点是什么?

  2. 在什么情况下,forEach更为可取。为什么它甚至进入JavaScript?为什么需要它?


解决方案

forEach 是一种方法在 Array 原型。它遍历数组的每个元素并将其传递给一个回调函数。

所以基本上, forEach 是用例传递数组中每个元素的简写方法转换为函数。这是一个常见的例子,我认为 Array.forEach 相对于 for 循环相当有用:

  // document.querySelectorAll的快捷方式
函数$$(expr,con){
return Array.prototype.slice.call((con | | document).querySelectorAll(expr));


//隐藏元素
函数hide(el){
el.style.display ='none';


//通过forEach隐藏所有div
$$('div')。forEach(hide);

//隐藏所有的$ div $ $ hide(divs [i])
}

正如您所看到的, forEach语句与循环的相比有所改进。另一方面,语句的更加灵活:它不一定涉及数组。对于循环来说,正常的性能稍微好一点,因为不涉及每个元素的函数调用。尽管如此,当它被写为 forEach 语句时,建议避免循环的


I am wondering: Is there any significant difference between forEach and for loop in JavaScript.

Consider this example:

var myArray = [1,2,3,4];

myArray.forEach(function(value) {
  console.log(value);
});

for (var i = 0; i < myArray.length; i++) {
  console.log(myArray[i]);
}

Here is part of my research:

  1. Performance: According to JsPerf : forEach is little slower than for loop.
  2. Usability: There is no way we can break/return from the callback in case of forEach loop.

For example: You want to find out if a number is prime or not. I think using for loop is much more easier than using forEach loop to do this.

  1. Readability: Using for loop makes code more readable than having forEach in code.
  2. Browser compatibility: forEach is Not supported in IE < 9 So that introduces some shim in our code.

My questions are:

  1. What are the advantages of forEach over for loop ?
  2. In what scenarios, forEach is more preferable.
  3. Why did even it come into JavaScript ? Why was it needed at all ?

解决方案

forEach is a method on the Array prototype. It iterates through each element of an array and passes it to a callback function.

So basically, forEach is a shorthand method for the use-case "pass each element of an array to a function". Here is a common example where I think Array.forEach is quite useful, compared to a for loop:

// shortcut for document.querySelectorAll
function $$(expr, con) {
    return Array.prototype.slice.call((con || document).querySelectorAll(expr));
}

// hide an element
function hide(el) {
    el.style.display = 'none';
}

// hide all divs via forEach
$$('div').forEach(hide); 

// hide all divs via for
for (var divs = $$('div'), i = 0; i < divs.length; i++) {
    hide(divs[i])
}

As you can see, the readability of the forEach statement is improved compared to a for loop.

On the other hand, a for statement is more flexible: it does not necessarily involve an array. The performance of a normal for loop is slightly better, because there is no function call for each element involved. Despite of this, it is recommended to avoid for loops when it can be written as a forEach statement.

这篇关于forEach和for循环之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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