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 width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.