迭代器使对象可以在for of中使用
需要一个next方法返回{done:,value:}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| let range = { from: 1, to: 5 };
range[Symbol.iterator] = function() {
return { current: this.from, last: this.to,
next() { if (this.current <= this.last) { return { done: false, value: this.current++ }; } else { return { done: true }; } } }; };
for (let num of range) { alert(num); }
|