ES6 Iteration Protocol
This is the part of a series of blogs on the new features on the upcoming ECMAScript 6 (ES6) specification which JavaScript implements.
In this blog we focus on the iteration protocol introduced in ES6.
Iteration
Lets take a quick programming 101 background. An iterator is basically a sequence generator. A sequence is an ordered list of values whose order and uniqueness does not matter. A trivial example of a sequence is the Array
.
So an iterator is just an abstraction of, for example, a for loop
. It has certain functions such as prev
, next
, valid
, key
, and value
to go through the sequence, iterating through the elements.
We do the iteration to save memory by only generating values that are needed when they are needed. This is done by dynamically performing instructions based on the iteration.
Think about a for loop that ends when i > 5
and breaks;
rather than going through the...