const banji = {
name: '终极一班',
stus: [
'xiaoming',
'xiaohu',
'xiaotian',
'xiaolaohu'
],
[Symbol.iterator]() {
let index = 0
let _this = this
return {
next() {
if (index < _this.stus.length) {
const result = { value: _this.stus[index], done: false }
index++
return result
} else {
return { value: undefined, done: true }
}
}
}
}
}
for (let iterator of banji) {
console.log(iterator)
}
遇到的问题:
- 没有把next方法变成箭头函数
- 把if内的代码合并成一行导致index++在return后,导致代码无限增加