April 2018
Beginner
536 pages
13h 21m
English
An iterator is a behavioral design pattern that is common in OOP. An iterator is an object that implements an interface such as the following one:
interface Iterator<T> {
next(value?: any): IteratorResult<T>;
return?(value?: any): IteratorResult<T>;
throw?(e?: any): IteratorResult<T>;
}
The preceding interface allows us to retrieve the items available in a collection. The iterator result allows us to know if we have reached the last item in the collection and to access the values in the collection:
interface IteratorResult<T> {
done: boolean;
value: T;
}
We can create custom iterators by implementing the IterableIterator interface. We will need to implement the next method and a method named Symbol.iterator:
class Fib implements ...