简述js中 for in 与 for of 区别

xiaoxiao2021-02-28  104

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
转载请注明原文地址: https://www.6miu.com/read-65638.html

最新回复(0)