ecmascript:iteration
差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン前のリビジョン次のリビジョン | 前のリビジョン | ||
ecmascript:iteration [2025/09/08 15:40] – nullpon | ecmascript:iteration [2025/09/08 15:46] (現在) – nullpon | ||
---|---|---|---|
行 51: | 行 51: | ||
console.log(Array.from(iterable)); | console.log(Array.from(iterable)); | ||
``` | ``` | ||
+ | |||
+ | 同じオブジェクトで反復可能プロトコル、イテレータプロトコル両方を実装しても構わない | ||
+ | |||
+ | ```javascript | ||
+ | const iteration = { | ||
+ | count: 0, | ||
+ | next() { | ||
+ | if (this.count < 5) { | ||
+ | return { value: this.count++, | ||
+ | } | ||
+ | return { value: undefined, done: true }; | ||
+ | }, | ||
+ | [Symbol.iterator]() { | ||
+ | return this; | ||
+ | }, | ||
+ | }; | ||
+ | |||
+ | |||
+ | for (const num of iteration) { | ||
+ | console.log(num); | ||
+ | } | ||
</ | </ |
ecmascript/iteration.1757346046.txt.gz · 最終更新: by nullpon