Home > Article > Web Front-end > Detailed explanation of iterators and generators in JavaScript_javascript skills
Processing each item in a collection is a very common operation. JavaScript provides many ways to iterate over a collection, from simple for and for each loops to map(), filter() and array comprehensions. ). In JavaScript 1.7, iterators and generators bring new iteration mechanisms to the core JavaScript syntax, and also provide a mechanism to customize the behavior of for...in and for each loops.
Iterator
An iterator is an object that accesses one element in a collection sequence at a time and keeps track of the current position of the iteration in the sequence. In JavaScript, an iterator is an object that provides a next() method that returns the next element in the sequence. This method throws a StopIteration exception when all elements in the sequence have been traversed.
Once an iterator object is created, it can be called explicitly by repeatedly calling next(), or implicitly using JavaScript's for...in and for each loops.
Simple iterators for iterating over objects and arrays can be created using Iterator():
Once initialization is complete, the next() method can be called to access the object's key-value pairs in sequence:
The for…in loop can be used instead of explicitly calling the next() method. The loop automatically terminates when the StopIteration exception is thrown.
If you only want to iterate the key value of the object, you can pass the second parameter to the Iterator() function with the value true:
One benefit of using Iterator() to access objects is that custom properties added to Object.prototype will not be included in the sequence object.
Iterator() can also be used on arrays:
Just like traversing an object, passing true as the second parameter will result in the traversal being the array index:
Use the let keyword to assign indexes and values to block variables inside the loop, and you can also use Destructuring Assignment:
Declare a custom iterator
Some object representing a collection of elements should be iterated over in a specified way.
1. Iterating an object representing a range should return the numbers contained in the range one by one
2. The leaf nodes of a tree can be accessed using depth-first or breadth-first
3. Iterating over an object representing the results of a database query should be returned row by row, even if the entire result set has not yet been loaded into a single array
4. An iterator acting on an infinite mathematical sequence (like the Fibonacci sequence) should return results one after another without creating an infinite-length data structure
JavaScript allows you to write custom iteration logic and apply it to an object
We create a simple Range object containing low and high values:
Now we create a custom iterator that returns a sequence containing all the integers in the range. The iterator interface requires us to provide a next() method to return the next element in the sequence or throw a StopIteration exception.
Our RangeIterator is instantiated with a range instance and maintains a current property to track the current sequence position.
Finally, in order for RangeIterator to be combined with Range, we need to add a special __iterator__ method for Range. It will be called when we try to iterate over a Range and should return a RangeIterator instance that implements the iteration logic.
Once we have completed our custom iterator, we can iterate over a range instance:
Generators: a better way to build iterators
Although custom iterators are a useful tool, careful planning is required when creating them because their internal state needs to be maintained explicitly.
The generator provides very powerful functions: it allows you to define a function that contains its own iteration algorithm, and it can automatically maintain its own state.
Generators are special functions that can serve as iterator factories. If a function contains one or more yield expressions, it is called a generator (Translator's Note: Node.js also needs to add * in front of the function name to indicate it).
Note: Only code blocks contained in