Skip to main content

Posts

Showing posts from April, 2019

Unpractical use cases - Iterators

** Unpractical use cases - Iterators What is an iterator? Array Iterator Objects An Array Iterator is an object, that represents a specific iteration over some specific Array instance object. There is not a named constructor for Array Iterator objects. Instead, Array iterator objects are created by calling certain methods of Array instance objects. ref : cma-262 Instead of trying to understand it, lets just look at a example Here we have an object that contains an array, to use the iterator on the array you can simply do something like To use the iterator to move to the next item in the array you call .next() console.log(theArrayIterator.next()) // x 5 { value: 'all', done: false } { value: 'the', done: false } { value: 'array', done: false } { value: 'values', done: false } { value: undefined, done: true } As you can see, each time you call next, it prints the value of the next item in the array, and once it reaches the end of t...