In ES6, Iterator and for... of... traverse Usage Analysis, es6for....
This example describes how to use Iterator and for... of... traversal in ES6. We will share this with you for your reference. The details are as follows:
Iterator and for... of... Traversal
1. Iterator Concept
Iterator is an interface that provides a unified access mechanism for different data structures. Some data structures in JS have native Iterator interfaces. To better understand this concept, we can also write an Iterator ourselves.
var it = simIteractor(['hi','ES5']);console.log(it.next()); //Object {value: "hi", done: false}console.log(it.next()); //Object {value: "ES5", done: false}console.log(it.next()); //Object {value: undefined, done: true}function simIteractor(array){ var nextIndex = 0; return{ next: function(){ return nextIndex < array.length ? {value: array[nextIndex++], done: false} : {value: undefined, done:true}; } };}
2. ES6 stipulates that the default Iterator interface is deployed in the Symbol. iterator attribute of the data structure, or a data structure can be traversed as long as it has the Symbol. iterator attribute. In ES6, three types of data structures are native with Iterator interfaces: arrays, some objects similar to arrays, Set and Map.
3. When it comes to traversal, let's talk about the Traversal method.
For... in...: for-in is designed for common objects. You can traverse string keys, so it is not applicable to array traversal.
For... of...: the for-of loop is used to traverse data-such as values in an array. The for-of loop can also traverse other sets.
The for-of loop not only supports arrays, but also supports most class array objects, such as DOMNodeList.
The for-of loop also supports string traversal, which treats the string as a series of Unicode characters for traversal:
Or (var chr of "abc") {alert (chr); // pop up a, B, c} in sequence}
It also supports Map and Set object traversal. If you do not know Map please see http://www.bkjia.com/article/110048.htm, if you do not know set please see http://www.bkjia.com/article/110052.htm.
I hope this article will help you design the ECMAscript program.