Home  >  Article  >  Web Front-end  >  Explanation of generator functions in ES6 (code examples)

Explanation of generator functions in ES6 (code examples)

不言
不言forward
2018-11-14 16:39:111874browse

This article brings you an introduction to the use of validator in laravel (code examples). It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Overview

In fact, I don’t know how to write the first few chapters, because they always involve some deeper things, such as Iterable objects, Generator, Iterator and the like, it might be better to organize these things after finishing this series. Now just write wherever you organize them

Grammar

function* name([param[, param[, ... param]]])
{ 
    statements 
}

Use function* as the declaration identifier, indicating that this is a generator function

name is the function name

param is the parameter name, which can be 255

Statements are the body of the function

Features

The generator function can be paused during execution, and can be continued when needed

Returned by the generator function It is an iterator object and will not be executed immediately

The generator function can use return, but it will no longer be iterable after return

Lizi-Infinite ID Grower

function* idMaker(begin=0){
  while(true)
    yield begin++;
}
let maker=idMaker(0)
console.log(maker.next().value) // 0
console.log(maker.next().value) // 1
console.log(maker.next().value) // 2
console.log(maker.next().value) // 3
...

Explanation :

Calling the generator function returns an iterator. An iterator is an object that satisfies the iterator protocol. Simply put, the iterator object must have a next function, which returns an object. We Called the iteration result object, the object contains the following two attributes:

  1. value: The result of this iteration

  2. done : Boolean value. If it is true, it means it cannot continue to iterate. If it is false, it means it can continue iteration.

Calling the next function will actually execute the generator function until it encounters yield and returns the iteration result object

Chestnut-Limited idGrower

function* idMaker(begin=0){
  while(begin<3)
    yield begin++;
}
let maker=idMaker(0)
console.log(maker.next()) // {value: 0, done: false}
console.log(maker.next()) // {value: 1, done: false}
console.log(maker.next()) // {value: 2, done: false}
console.log(maker.next()) // {value: undefined, done: true}

If the function execution is completed, then done will automatically become true

next receives parameters

function *createIterator() {
    let first = yield 1;
    let second = yield first + 2; // 4 + 2 
                                  // first =4 是next(4)将参数赋给上一条的
    yield second + 3;             // 5 + 3
}

let iterator = createIterator();

console.log(iterator.next());    // "{ value: 1, done: false }"
console.log(iterator.next(4));   // "{ value: 6, done: false }"
console.log(iterator.next(5));   // "{ value: 8, done: false }"
console.log(iterator.next());    // "{ value: undefined, done: true }"

This is quite troublesome to explain , the first call to iterator.next() returns 1, and the code stops at yield 1, but we save the result of yield 1 in the first variable. In fact, this operation does not happen. When we execute iterator for the second time .next(4), note that a parameter 4 is passed here. At this time, 4 will be regarded as the return value of yield 1 and assigned to first, so the second execution can be regarded as an execution sequence of let first=4.

return

function* yieldAndReturn() {
  yield "1";
  return "2";
  yield "3"; 
}
var gen = yieldAndReturn()
console.log(gen.next()); // { value: "1", done: false }
console.log(gen.next()); // { value: "2", done: true }
console.log(gen.next()); // { value: undefined, done: true }

The above is the detailed content of Explanation of generator functions in ES6 (code examples). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete