//表示"集合"的数据结构:数组Array,对象Object,Map和Set
//通过引入Iterator一种机制,为各种不同的数据结构提供统一的访问接口
//作用
//为各种数据结构的遍历,提供一个统一,简便的访问方式
//ES6创造了一种新的遍历命令For...of循环
let target = ['a', 'b', 'c']
//返回一个遍历器
var myIterator = myIterator(target)
console.log(myIterator.next())
console.log(myIterator.next())
console.log(myIterator.next())
console.log(myIterator.next())
function myIterator(array) {
var nextIndex = 0
return {
next: function () {
return nextIndex < array.length ? { value: array[nextIndex++], done: false } :
{ value: undefined, done: true }
}
}
}