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 31 32 33 |
'use strict'; const p = console.log; const iterable1 = { [Symbol.iterator]: () => { let cnt = 0; const iterator = { next: () => { const value = cnt++; const done = (5 < cnt); return { value, done }; // iterator result } }; return iterator; }, }; const iterable2 = { [Symbol.iterator]: function* () { for (let i = 0; i < 5; i++) { yield i; } } }; for (const v of iterable1) { p(v); } for (const v of iterable2) { p(v); } |
1 2 3 4 5 6 7 8 9 10 |
0 1 2 3 4 0 1 2 3 4 |