Skip to main content

A simple understanding of ES6 iterators

 What is an iterator?An iterator is an interface that provides a unified access mechanism for various data structures. Any data structure can complete the traversal operation as long as the iterator interface is deployed.ES6 created a new traversal command for...of loop, which natively has a data structure with the iterator interface (which can be traversed with for...of). Contains Array, Arguments, Set, Map, String, TypedArray, NodeList. Of course, you can also implement this interface manually, which is convenient for practical use.

Working Mechanism:

  • Creates a pointer object pointing to the start of the current data structure.
  • The first time the object's next method is called, the pointer automatically points to the first member of the data structure.
  • Next, the next method is called continuously, and the pointer moves backward until it points to the last member.
  • Each call to the next method returns an object containing the value and done properties.

Preliminary Understanding:

let str=[1,2,3];
console.log(str);
let iterator=str[Symbol.iterator]();
let iteratorValue=str[Symbol.iterator]().next();
console.log(iterator);
console.log(iteratorValue);

The following is to manually implement the iterator, mainly to return an object, and then a pointer field and end mark to move down.

let obj={
    id:100,
    hobby:["sing", "dance", "rap", "basketball","music~"],
    [Symbol.iterator](){
        let index=0;
        let result={};
        return{
            next:()=>{
                if(index<this.hobby.length){
                    result={value:this.hobby[index],done:false};
                }
                else{
                    result={value:undefined,done:true};
                }
                index++;
                return result;
            }
        }
    }
}
for(let value of obj){
    console.log(value);
}

Summarize:

Iterators are an important addition to ES6, providing an interface that can be implemented by any object to continuously return each value of the object being iterated over. Objects implementing the Iterable interface have a Symbol.iterator property that references the default iterator. An object is considered iterable if it has Symbol.iterator on it.

Comments

Popular posts from this blog

What is the difference between the box-size property content-box and border-box in the css box model?

 The box model is a very important concept in CSS layout, it includes content area, padding, border, and margin. Box models can be divided into two types: standard box models and IE box models. The box model, as the name suggests, is used to hold things, and the things it holds are the content of HTML elements. In other words, every visible HTML element is a box.

Js uses recursive way to traverse the dom tree to dynamically create element nodes

 What is a dom tree? In short, DOM is the Document Object Model, which provides a structured representation for documents and defines how to access the document structure through scripts. DOM is composed of nodes. After the HTML is loaded, the rendering engine will generate a DOM tree in memory based on the HTML document. This article uses a small case to traverse the dom tree recursively. The core of the method is to determine whether the incoming data is an array, and then traverse the root node. Note that there must be an end condition when using recursion.