Home » JavaScript » ES6-Iterator and for..Of.. Traversal

ES6-Iterator and for..Of.. Traversal

ES6-Iterator and for..Of.. Traversal, there is a need for friends, you can refer to.

Iterator, and for..Of.. Traverse

1.Iterator concept


traversal (Iterator) is an interface that provides a unified access mechanism for various data structures.

JavaScript some data structure with Iterator interface native. In order to better understand this concept, we can write a Iterator.

, 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;
Next:, function () {
Return, nextIndex, < array.length, {value:, array[nextIndex++], done:, false}: {value:, undefined, done:true};

2.ES6, the default Iterator Symbol.iterator interface deployed in attribute data structure or a data structure, so long as has the Symbol.iterator attributes can task can be traversed. In ES63 >, a kind of data structure with Iterator array. The object of some similar arrays, Set.

> >

3. refers to ergodic, it is about the traversal method,.

for... In: for-in is designed for plain objects. You can iterate through the keys to get string type, so it doesn't apply to array traversal of.

For... Of: for-of cycles are used to traverse data - such as values in the array,.for-of cycles, and other


for-of cycle not only support array, also support the most array object, such as DOMNodeList. >

for-of loop also supports string traversal, which treats strings as a series of Unicode characters:

or (varchrof, ABC) {

alert (CHr); // pops up a, B, c

in turn


It also supports

SetMap > object traversal. If you don't know Map see http://blog.csdn.net/qiqingjin/article/details/51114769, if you don't know Set see http://blog.csdn.net/qiqingjin/article/details/51115171.