-
Notifications
You must be signed in to change notification settings - Fork 0
迭代协议 #10
Copy link
Copy link
Open
Description
const obj = {'a':'a','b':'b'};
// obj[Symbol.iterator] 是可迭代协议
obj[Symbol.iterator] = function() {
const that = this;
const keys = Object.keys(this);
// 返回的对象则是迭代器协议
return {
next: function() {
if (this._idx !== keys.length) {
return { value: that[keys[this._idx++]], done: false };
} else {
return { done: true };
}
},
_idx: 0,
};
};
for (const element of obj) {
console.log(element);
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels