标签:自己 foreach value this OLE iterator 遍历数组 eve 枚举
var myArray = [1,2,3]; for(var i=0; i<myArray.length; i++) { console.log(myArray[i]) } // 1 2 3
ES5 增加了数组的辅助迭代器,包括 forEach(...)、every(...)、some(...)
var myArray = [1,2,3]; for(var v of myArray){ console.log(v) } // 1 2 3
for..of 循环首先会向被访问对象请求一个迭代器对象,然后通过调用迭代器对象的 next() 方法来遍历所有返回值
var myArray = [1,2,3]; var it = myArray[Symbol.iterator](); it.next(); // {value: 1, done: false} it.next(); // {value: 2, done: false} it.next(); // {value: 3, done: false} it.next(); // {done: true}
var myObj = { a:2, b:3 } Object.defineProperty(myObj, Symbol.iterator, { enumerable: false, writable: false, configurable: true, value: function() { var o = this; var idx = 0; var ks = Object.keys(o); return { next: function() { return { value: o[ks[idx++]], done: (idx > ks.length) } } } } }) var it = myObj[Symbol.iterator](); it.next(); // {value: 2, done: false} it.next(); // {value: 3, done: false} it.next(); // {done: true}
标签:自己 foreach value this OLE iterator 遍历数组 eve 枚举
原文地址:https://www.cnblogs.com/wzndkj/p/12596368.html