The iterable protocol
The iterable protocol allows JavaScript objects to define or customize their iteration behavior, such as what values are looped over in a for..of construct. Some built-in types are built-in iterables with a default iteration behavior, such as Array or Map, while other types (such as Object) are not.
In order to be iterable, an object must implement the @@iterator method, meaning that the object (or one of the objects up its prototype chain) must have a property with a @@iterator key which is available via constant Symbol.iterator:
Property | Value |
---|---|
[Symbol.iterator] | A zero arguments function that returns an object, conforming to the iterator protocol. |
The iterator protocol
The iterator protocol defines a standard way to produce a sequence of values (either finite or infinite).
An object is an iterator when it implements a next() method with the following semantics: