[转] 简述js中 for in 与 for of 区别

for in是ES5标准,遍历key. 
for of是ES6标准,遍历value.

for (var key in arr){
    console.log(arr[key]);
}

for (var value of arr){
    console.log(value);
}

 

一个比较神奇的例子:

Object.prototype.objCustom = function () {}; 
Array.prototype.arrCustom = function () {};

let iterable = [3, 5, 7];
iterable.foo = "hello";

for (let i in iterable) {
  console.log(i); //  0, 1, 2, "foo", "arrCustom", "objCustom"
}


for (let i of iterable) {
  console.log(i); // 3, 5, 7

posted @ 2018-04-08 10:38  {前端开发}  阅读(24171)  评论(0编辑  收藏  举报