修改for..of的两种方式
js
let obj = {
a: 'a',
b: 'b',
c: 'c'
};
Object.prototype[Symbol.asyncIterator] = function () {
let index = 0;
let obj = this;
return {
next() {
if (index < Object.keys(obj).length) {
return { value: obj[Object.keys(obj)[index++]], done: false };
} else {
return { value: undefined, done: true };
}
}
};
};
Object.prototype[Symbol.iterator] = function* () {
for (let key of Object.keys(this)) {
yield this[key];
}
};
for await (const x of obj) {
console.log(x);
}
const [a, b, c] = obj;
console.log(a, b, c);