JavaScript Iterators
JavaScript Iterators were added in ECMAScript 2015(ES6).
It allows JavaScript objects to specify how custom objects can be used
in for...of
loops.
For a object to be iterable
it must implement iteratorMethod
.
When object needs to be iterated is iteratorMethod
is called
with no arguments. The iteratorMethod
returns a iterator
.
The object must have a property which is available via a constant Symbol.Iterator
.
The value of the property must be the iterator
method.
An object is a iterator
if it has a next
function which is a zero
argument function that returns a object with atleast two properties
done(boolean)
- Has value true
when the iterator has completed its sequence.
Has value false
if the iterator was able to produce next value in the sequence.
value
- Any JavaScript value returned by the iterator.