//先声明一个要遍历的对象
const STUDENT = {
name: '丁七岁',
hobbies: [
'吃饭',
'睡觉',
'打豆豆',
'听音乐'
],
[Symbol.iterator]() {
//索引变量
let index = 0
let _this = this //做一个this的保存
return {
next: function () {
if (index < _this.hobbies.length) {
const result = {
value: _this.hobbies[index],
done: false,
}
index++ //下标自加
return result
} else {
return {
value: undefined,
done: true
}
}
}
}
}
}
//遍历这个对象
for (let v of STUDENT){
console.log(v)
}
//吃饭
// 睡觉
// 打豆豆
// 听音乐