🔰iteration
JS ⟩ iteration
three types that are related to iteration:
iterable - object that can make iterators to loop over itself.
iterator - object that can produce the next iteration result.
iteration result - object that holds the result of each step of the iteration.
custom extension
next() - iterators must implement next() method.
make-iterator method of an iterable - iterables can make iterators.
iterators only iterate once❗️ - iterators only iterate once.
iterable iterator - an iterator that is itself iterable.
IteratorPrototype - prototype of all built-in iterators.
Iterator - extension for (built-in) iterators.
three types that are related to the iteration.
iterable - object that can be iterated.
iterator - object that performs the iteration.
iteration result - object that holds the result of each step of the iteration.
Generator - objects returned by generator function.
generator function - return .
💾 replit:iterator
let iterable = [1, 2, 3];
// ⭐️ iterate an iterable "the hard way"
let iterator = iterable[Symbol.iterator](); // ⭐️ 1. make an iterator
let result = iterator.next(); // ⭐️ 2. first result
while (!result.done) {
console.log(result.value);
result = iterator.next(); // call next() repeatedly
}
// ⭐️ the easy way
for (const i of iterable) { console.log(i) } // 1, 2, 3Symbol.iterator ( = makeIterator in Swift )
ExploringJS ⟩ 21. Iterables and Iterators ⭐️
JavaScript: The Definitive Guide ⟩ 12. Iterators and Generators
JS.info ⟩
Last updated
Was this helpful?