# 迭代器
定义 for of 的行为
迭代器是一个对象,通过 next() 方法实现迭代任何一个对象
var string = '123456789'
string.__proto__[Symbol.iterator] = function() {
let index = 0
const _t = this
return {
next: function() {
const value = _t.slice(index, index + 2)
const result = { value , done: index >= _t.length }
index += 2
return result
}
}
}
for (let v of string) {
console.log(v)
}
const iterator = string[Symbol.iterator]()
iterator.next()