The Iterator pattern lets you walk through a collection without needing to know how it's stored internally (array, linked list, tree). The collection hands you an iterator, and you just call next().
class Iterator {
constructor(items) {
this.items = items;
this.index = 0;
}
next() {
return this.items[this.index++];
}
hasNext() {
return this.index < this.items.length;
}
}
class IterableCollection {
constructor(items) {
this.items = items;
}
createIterator() {
return new Iterator(this.items);
}
}-
Iterator Class
- Constructor: Initializes the
itemsarray and sets the startingindexto 0. next()Method: Returns the current item and increments the index.hasNext()Method: Checks if there are more items to iterate over.
- Constructor: Initializes the
-
IterableCollection Class
- Constructor: Initializes the
itemsarray. createIterator()Method: Creates and returns a newIteratorinstance for the collection.
- Constructor: Initializes the
// Usage
const collection = new IterableCollection(["item1", "item2", "item3"]);
const iterator = collection.createIterator();
while (iterator.hasNext()) {
console.log(iterator.next());
}- Creating Collection: An instance of
IterableCollectionis created with an array of items. - Creating Iterator: An iterator is created for the collection using
createIterator(). - Iterating: A
whileloop is used to iterate over the collection, printing each item until there are no more items.
The iterator keeps all traversal state (the current index) out of the collection itself. That means you can have multiple iterators on the same collection at the same time without them interfering. Worth noting: in modern JavaScript you'd usually use a generator or the built-in Symbol.iterator protocol, but rolling your own like this makes the mechanics easy to see.