[轉] 簡述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