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

相关文章:

  • 2022-12-23
  • 2021-05-16
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-08-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-25
相关资源
相似解决方案