es6 iterator and generator study notes

1. iterator iterator

Array, Set, Map, NodeList objects can use for-of loops because of the default iterator property. Objects do not have default iterators.

Iterators can be defined using Symbol.iterator.

The background of the iterator: Although the loop statement is simple, if multiple loops are nested, multiple variables need to be tracked, and the complexity of the code will be greatly increased. Causes programs to fail, and iterators are designed to remove this complexity and reduce bugs in loops.

An iterator is a special kind of object. Each iterator object has a next() method. Each time this method is called, an object is returned. This object includes two properties: value, which represents the next value to be returned, and done : Boolean value, returns true when there is no more data to return, and returns true later

 

2. generator generator

A generator is a function that returns an iterator, and the new keyword yield is used in the function. The format is as follows

function *fname(){

  yield 1;

  yield 2;

}

let iterator = fname();

console.log(iterator.next().value);   //1

The most interesting thing about generators is that after each yield statement is executed, the function will automatically stop executing, and will not continue until the next() method of the iterator is called again.

Restrictions on the use of yield: The yield keyword can only be used inside a generator, and using it in other places will cause the program to throw a syntax error

function *createIterator(items){

  items.forEach(function(item){

    //Grammatical errors

    yield item+1;

  })

}

It cannot penetrate function boundaries like return

Generators can also be defined via function expressions

var createIterator = function *(){

  ..

  yield 1;

}

You can also define generators in objects

let o = {

  createIterator: function *(){

    yield 1;

  }

}

let o = {

  *createIterator(){

    yield 1;

  }

}

Note: You cannot use arrow functions to create generators. Why? What are the differences between arrow functions and ordinary functions:

First of all, there is no binding of this, arguments, super and new.target, these values ​​are determined by the non-arrow function of the nearest layer in the periphery.

Cannot be called with the new keyword, arrow functions do not have a construct method

There is no prototype, there is no prototype property

The binding of this cannot be changed, the value of this inside the function cannot be changed (because it is the outer this), and it is always consistent throughout the life cycle of the function.

3. Iterables and for-of loops

Iterable objects have the Symbol.iterator property, which is an object closely related to iterators

Symbol.iterator can return an iterator that acts on the attached object through the specified function:

let values = [1,2,3];

let iterator = values[Symbol.iterator]();

console.log(iterator.next());

 

In ES6, all collection objects (arrays, Set collections, Map collections) and strings are iterables. for-of requires these functions of iterable objects.

Each execution of the for-of loop will call the next() method of the iterable object and store the value property of the result object returned by the iterator in a variable. The loop will continue to execute this process until the value of the done property of the object is returned. is true.

Using a for-of loop with non-iterable objects, null or undefined will cause the program to throw an error.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324456676&siteId=291194637