Reputation: 81
When I implement the interface array, there are methods there return an IterableIterator. The IterableIterator extends from Iterator that I can understand well and makes sense to me.
interface IterableIterator<T> extends Iterator<T> {
[Symbol.iterator](): IterableIterator<T>;
}
But what should I implement for Symbol.iterator. This also return an IterableIterator (recursion?) I don't understand the concept of IterableIterator. It would be great if someone could respond. Or could give me a source where this is described.
Upvotes: 8
Views: 20385
Reputation: 11354
It is often useful for Iterator
objects to become Iterable
themselves, so that they can:
for..of
loopArray.from
new Set
new Map
If an Iterator
object is not Iterable
, then they can’t be used in ways described above. The only way to iterate through it is to repeatedly call the .next()
method. This is inconvenient. Therefore, most Iterator
objects produced by ECMAScript APIs are also Iterable
.
To make an Iterator
Iterable
, the [Symbol.iterator]
method should return this
.
For more information see:
Upvotes: 12