![]() Calling next() again after an iterator has reached the end of the itarable will result in a TypeError being thrown. The value for done only evaluates to true after another next() call once the last value has been returned, so to go over the entire iterable there will always be one more call to next() than there is data in the iterable. ![]() ![]() next ( ) ) // TypeError: Cannot read property 'length' of undefinedĪs you can see, the iterator has a next() method that returns the next value in the iterable. The value indicates the current data value pointed by the iterator and done is a boolean that tells us if the iterator has reached the last element in the data structure. In a way, the erator can be compared to an iterator factory that produces an iterator whenever the data structure is placed in a loop.Īs an iterator moves over the data structure and provides the elements sequentially, the object returned by the iterable contains a valueand a done property. This iterable property is namespaced as erator and the object that it returns can be used on a common interface that is shared by all looping control structures. When the array is used in a for.of loop, the iterable property is called which returns an iterator. The concept of the iterable protocol can be split into the iterable, the data structure itself, and the iterator, a pointer that moves over the iterable. ![]() Imagine a self-packaged data structure that unloads data one-by-one in order when put inside of a for.of loop. Iterables are data structures which provide a mechanism to allow other data consumers to publicly access its elements in a sequential manner. While JavaScript provides maps, arrays and sets with an iterable property by default, regular JavaScript objects do not have this by default. This is referred to as the iterable and the data structures that support this functionality are called iterables. JavaScript supports looping through data set objects such as arrays using control structures such as for…of and the spread operator.
0 Comments
Leave a Reply. |