In ES6, Iterator and for... of... traverse Usage Analysis, es6for....

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.