mirror of
https://github.com/ruanyf/es6tutorial.git
synced 2025-05-24 18:32:22 +00:00
docs(generator): edit yield*
This commit is contained in:
parent
bb828d8b63
commit
d2e69f0e3e
@ -764,7 +764,7 @@ gen.return(2); // Object {value: 2, done: true}
|
|||||||
|
|
||||||
## yield\* 表达式
|
## yield\* 表达式
|
||||||
|
|
||||||
如果在 Generator 函数内部,调用另一个 Generator 函数,默认情况下是没有效果的。
|
如果在 Generator 函数内部,调用另一个 Generator 函数。需要在前者的函数体内部,自己手动完成遍历。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
function* foo() {
|
function* foo() {
|
||||||
@ -774,20 +774,25 @@ function* foo() {
|
|||||||
|
|
||||||
function* bar() {
|
function* bar() {
|
||||||
yield 'x';
|
yield 'x';
|
||||||
foo();
|
// 手动遍历 foo()
|
||||||
|
for (let i of foo()) {
|
||||||
|
console.log(i);
|
||||||
|
}
|
||||||
yield 'y';
|
yield 'y';
|
||||||
}
|
}
|
||||||
|
|
||||||
for (let v of bar()){
|
for (let v of bar()){
|
||||||
console.log(v);
|
console.log(v);
|
||||||
}
|
}
|
||||||
// "x"
|
// x
|
||||||
// "y"
|
// a
|
||||||
|
// b
|
||||||
|
// y
|
||||||
```
|
```
|
||||||
|
|
||||||
上面代码中,`foo`和`bar`都是 Generator 函数,在`bar`里面调用`foo`,是不会有效果的。
|
上面代码中,`foo`和`bar`都是 Generator 函数,在`bar`里面调用`foo`,就需要手动遍历`foo`。如果有多个 Generator 函数嵌套,写起来就非常麻烦。
|
||||||
|
|
||||||
这个就需要用到`yield*`表达式,用来在一个 Generator 函数里面执行另一个 Generator 函数。
|
ES6 提供了`yield*`表达式,作为解决办法,用来在一个 Generator 函数里面执行另一个 Generator 函数。
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
function* bar() {
|
function* bar() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user