//自定义对象需要可迭代,需要创建iterator,迭代器对象需内置next函数,next函数返回一个{value:,done:}对象
const obj = {
name: "任意对象创建迭代器,实现迭代",
status: [
"TES", "FPX", "RNG", "EDG"
],
[Symbol.iterator]() {
let index = -1//index优先++了,需要内置为-1
return {
next: function () {
if (index < this.status.length) {
index++
return {
value: this.status[index], done: false
}
}
else {
return {
value: undefined, done: true
}
}
}.bind(this)
}
}
}
for (let data of obj) {
console.log(data)
}