JS-ES6 Study Notes-iterator

Source: Internet
Author: User
Tags iterable

1. The Iterator is an interface that provides a unified access mechanism for a variety of different data structures. Any data structure can be traversed (that is, all members of the data structure are processed sequentially) as long as the iterator interface is deployed.

2. Iterator has three functions: one is to provide a unified and simple access interface for various data structures, and the other is to make the members of the data structure arranged in some order; the third is ES6 created a new traversal command for...of loop, iterator interface is mainly for for...of consumption.

3, in the ES6, some data structures are natively equipped with iterator interfaces (such as arrays), that is, without any processing, it can be iterated for...of through, some will not (such as objects). The reason is that these data structures are natively deployed with Symbol.iterator attributes (see below), and some data structures are not. A Symbol.iterator data structure that deploys attributes is called a traversal interface. When this interface is called, a Walker object is returned.

4. In ES6, there are three types of data structures that are native to iterator interfaces: arrays, some array-like objects, set, and map structures.

5. An example of adding an iterator interface to an object.

Let obj ={data: [' Hello ', ' world '], [Symbol.iterator] () {const Self= This; Let Index= 0; return{Next () {if(Index <self.data.length) {return{Value:self.data[index++], Done:false          }; } Else {          return{value:undefined, Done:true };  }      }    }; }};

6. The following is an example of an array-like method for invoking an array of objects Symbol.iterator .

Let iterable = {  0: ' A ',  1: ' B ',  2: ' C ',  3,  [ Symbol.iterator]: Array.prototype[symbol.iterator]};  for (Let item of iterable) {  //  ' A ', ' B ', ' C '}

Note that normal objects do not have an Symbol.iterator effect on how to deploy arrays.

7, there are some occasions will be called by default iterator interface (that is Symbol.iterator , the method), in addition to the loop described below for...of , there are several other occasions.

    • Deconstruction Assignment
    • Extension operators (...)
    • Yield*_yield* is followed by a ergodic structure that invokes the iterator interface of the structure.
    • Since the traversal of the array invokes the Ergodic interface, any event that accepts an array as a parameter is actually called

8, the string is an array-like object, also native with the iterator interface.

9, the Walker object in addition to have next methods, you can also have return methods and throw methods. If you write the Walker object generation function yourself, then next the method is mandatory, and the methods return and throw methods are optional for deployment.

JS-ES6 Study Notes-iterator

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.