const person = {
name: '胡杰',
age: 18,
[Symbol('admirer')]: '小花'
}
// Object.prototype[Symbol.iterator] = function () {
// let _this = this
// const keys = Reflect.ownKeys(_this)
// let index = 0
// return {
// next() {
// if(index<keys.length) {
// return {value: _this[keys[index++]],done: false}
// } else {
// return {value: undefined, done: true}
// }
// }
// }
// }
Object.prototype[Symbol.iterator] = function () {
let that = this
const keys = Reflect.ownKeys(that)
const length = keys.length
let index = 0
return {
next() {
if (index <= length - 1) {
return {
value: that[keys[index++]],
done: false
}
} else {
return {
value: undefined,
done: true
}
}
}
}
}
console.log('---------for in----------')
for (key in person) {
console.log(person[key])
}
console.log('---------for of----------')
for (value of person) {
console.log(value)
}
console