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

Use of js array filter() method

 Arrays are frequently used in development, and processing data in arrays is one of the more common and important operations. Therefore, processing data in arrays during development is an important skill. Every developer Everyone should master the operations of arrays, especially for junior developers who have just entered the industry, so be sure to master the relevant skills. This article mainly shares some operations to filter the data in the array, about the use of the filter() method.

ES6 arrow functions

 In ES6, in addition to the new features of let and const, arrow functions are the most frequently used new features. But this feature is not unique to ES6. Arrow functions, as the name suggests, are functions defined using arrows (=>) and belong to a class of anonymous functions. It allows us to write smaller function syntaxes. The code of arrow functions is simpler and more flexible to write.