ES6 Generator (3) Summary of Knowledge Points (10)

The main purpose of taking notes is to improve the efficiency of learning, avoid reading but not doing it, not understanding the essence of the book, and ignoring the key knowledge in the book. The study in this article is mainly based on Ruan Yifeng, and I am very grateful to Ruan Yifeng for his selfless dedication. . If you want to know more about the content of this section, you can see Ruan Yifeng's ES6 official website http://es6.ruanyifeng.com/#docs/generator

yield*expression

If another Generator function is called inside a Generator function, the default case has no effect. This requires the use of yield* expressions, which are used to execute another Generator function inside a Generator function.

var g = function *(){
    yield "leo";
    yield "lalalal";

}
var p = g();
var g2 = function * (){
    yield "donna";
    yield* p;
    yield "henory";
}
var p2 = g2 ();
console.log(p2.next());//{ value: "donna", done: false }
console.log(p2.next());//{ value: "leo", done: false }
console.log(p2.next());//{ value: "lalalal", done: false }
console.log(p2.next());//{ value: "henory", done: false }
console.log(p2.next());//{ value: "undefined", done: true }

Note: If the yield expression is followed by a traverser object, you need to add an asterisk after the yield expression to indicate that it returns a traverser object, which is called a yield* expression.

The Generator function after the yield* expression (when there is no return statement) is equivalent to deploying a for...of loop inside the Generator function.

If the Generator function after the yield* expression has a return statement, you need to use var value = yield* iterator to get the value of the return statement

var g = function *(){
    yield "leo";
    yield "lalalal";
    return "over";

}
var p = g();


var g2 = function * (){
    yield "donna";
   var value = yield*p;
  // console.log(value);
    yield "henory";
}
var p2 = g2 ();
console.log(p2.next());
console.log(p2.next());
console.log(p2.next());
console.log(p2.next());
console.log(p2.next());
//{ value: "donna", done: false }
//{ value: "leo", done: false }
//{ value: "lalalal", done: false }
// over
//{ value: "henory", done: false }
//{ value: "undefined", done: true }

If yield* is followed by an array, since the array natively supports traversers, the array members will be traversed. If the yield expression is followed by an array, the entire array will be returned
var g2 = function * (){
    yield "donna";
   yield* ["leo","momo"];
   yield [1,2,3];
    yield "henory";
}
var p2 = g2 ();
console.log(p2.next());//{ value: "donna", done: false }
console.log(p2.next());//{ value: "leo", done: false }
console.log(p2.next());//{ value: "momo", done: false }
console.log(p2.next());//{ value: [1,2,3], done: false }
console.log(p2.next());//{ value: "henory", done: false }
In fact, any data structure can be traversed by yield* as long as it has an Iterator interface. (array, array-like, string, Map data structure, Set data structure)

If the property of an object is a Generator function, you can add an asterisk in front of the property to indicate that the property is a Generator function

The Generator function cannot be used with the new command, and an error will be reported.

Guess you like

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