links: YDK JS Chapter 3
Let’s look at the interface of iterator and iterable to understand them clearly
“An object is an Iterable when it confirms to Iterable interface”
interface Iterable {
[Symbol.iterator]() {
// ...
return Iterator;
}
}
In simple words, any object is iterable (iter+able meaning able to be iterated) if it contains a method name Symbol.iterator (Symbols can also define methods) that returns an Iterator.
What’s an Iterator?
“The Iterator object must confirm to Iterator interface.”
The Iterator object must have a method next() that returns an object with properties done (a boolean indicating the end of the operation) and value (the item extracted from the collection at the iteration)
interface Iterator {
next() {
//...
return {
value: <value>,
done: <boolean>
}
}
}
tags: javascript , fundamentals