实现代码
Object.prototype[Symbol.iterator] = function () {
let keys = Object.keys(this);
let index = 0;
return {
next: () => {
return {
value: this[keys[index++]],
done: index >= keys.length + 1 ? true : false,
}
}
}
}
var obj={
a:"skfjslkjdfs",
b:"sklfjlskjdfks"
}
for(let i of obj){
console.log(i,"iiii")
}
实现方式2,使用生成器实现
Object.prototype[Symbol.iterator] = function* () {
for (let key of Object.keys(this)) {
yield this[key];
}
};
var obj = {
a: "skfjslkjdfs",
b: "sklfjlskjdfks"
};
for (let i of obj) {
console.log(i, "iiii");
}