Generator function of the generator's new ES6

First, what is the generator function?

Generator function is one of the new features ES6, which can be a way to temporarily withdraw in the implementation, but also to re-enter later recall a functional continue execution.
And all state variables defined within a function is not affected by dropouts.

Second, grammar

Statement manner:
function name of the function * ([1 parameter [parameter 2 [, parameters ... n]]]) {JS statement}

Call Description:

It returns an iterator when the generator 1) generated call object
2) of the iterator has a next () method, which returns an object contains a value, and a done
3) each time the next call () method encounters expression yied formula suspended position
4) value represented yied expression returns the value, done indicates whether or not the generator has generated the last value
5) If a next () method passing parameter values, the parameter to be replaced by the return value of the expression yied
6) If you call return in the generator, the generator will be completed ahead of schedule

Third, examples

1, a self-defined function generator hyperplasia, each next () call returns the value of the increment

Output is as follows:

False 1
2 false
3 false
4 // Think about why false is false?
Undefined to true
what we've done things:

  1. Declare a generator function increment (n), is determined each time increment is less than n
  2. Each with a yield increment expression, the return value of the expression, then incremented (as is the suffix ++)

function* increment(n){
var index = 1;
while(index<n)
yield index++;
}

  1. Generator function call returns an iterator object to variable IT
    var INCREMENT IT = (. 5);

  2. for loop iterates
    // why would declare a temp variable?
    for (the let I = 0; I <. 5; I ++)
    {
    the let it.next TEMP = ();
    the console.log (temp.value);
    the console.log (temp.done);
    }

Directly it.next ()

for(let i = 0; i<5; i++)
{
console.log(it.next().value);
console.log(it.next().done);
}

//输出为:1, false, 3, false, undefined, true, undefined, true, undefined, true

2, to the next () passing parameters

function* increment(){
let index = 1;
console.log(yield index++);
console.log(yield index++);
console.log(yield index++);
}

var it = increment();

it.next();
it.next(3);
it.next(9);

Output: 3,9

3, explicit return

function* increment(){
let index = 1;
yield index++;
return “complete”;
yield index++;
yield index++;
}

var it = increment();

console.log(it.next().value); { value: 1, done: false }
console.log(it.next().value); { value: “complete”, done: true }
console.log(it.next().value); { value: undefined, done: true }

Fourth, supplement

yield*

grammar:

yield * [[expression]]

Description:

yield * expression for delegation to another generator or iterables.

chestnut:

function* increment_slowly(){
let index = 1.1;
yield index = 1.2;
yield index += 0.11;
}

function* increment(){
let index = 1;
yield index++;
yield* increment_slowly();
yield index++;
}

var it = increment();

console.log(it.next().value); // >1
console.log(it.next().value); // >1.2
console.log(it.next().value); // >1.31
console.log(it.next().value); // >2

Guess you like

Origin blog.csdn.net/weixin_43858880/article/details/90637777