1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class MyRange { constructor(count=10) { console.log('constructor, count:' + count); this.total = count; } [Symbol.iterator]() { return { next:() => { this.total = this.total - 1; return this.total <= 0 ? { done: true } : { value: this.total }; } } } } |
1 |
const test = new MyRange(6) |
constructor, count:6
1 2 3 |
for (const value of test) { console.log(value) } |
output:
6, 5, 4, 3, 2, 1