[转] 简述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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM