File tree 1 file changed +8
-8
lines changed
1 file changed +8
-8
lines changed Original file line number Diff line number Diff line change @@ -313,7 +313,7 @@ wrapped().next('hello!')
313
313
` for...of ` 循环可以自动遍历 Generator 函数时生成的` Iterator ` 对象,且此时不再需要调用` next ` 方法。
314
314
315
315
``` javascript
316
- function * foo () {
316
+ function * foo () {
317
317
yield 1 ;
318
318
yield 2 ;
319
319
yield 3 ;
@@ -912,16 +912,16 @@ read.next().value // "h"
912
912
如果被代理的 Generator 函数有` return ` 语句,那么就可以向代理它的 Generator 函数返回数据。
913
913
914
914
``` javascript
915
- function * foo () {
915
+ function * foo () {
916
916
yield 2 ;
917
917
yield 3 ;
918
918
return " foo" ;
919
919
}
920
920
921
- function * bar () {
921
+ function * bar () {
922
922
yield 1 ;
923
- var v = yield * foo ();
924
- console .log ( " v: " + v );
923
+ var v = yield * foo ();
924
+ console .log (" v: " + v);
925
925
yield 4 ;
926
926
}
927
927
@@ -1225,7 +1225,7 @@ JavaScript 代码运行时,会产生一个全局的上下文环境(context
1225
1225
Generator 函数不是这样,它执行产生的上下文环境,一旦遇到` yield ` 命令,就会暂时退出堆栈,但是并不消失,里面的所有变量和对象会冻结在当前状态。等到对它执行` next ` 命令时,这个上下文环境又会重新加入调用栈,冻结的变量和对象恢复执行。
1226
1226
1227
1227
``` javascript
1228
- function * gen () {
1228
+ function * gen () {
1229
1229
yield 1 ;
1230
1230
return 2 ;
1231
1231
}
@@ -1371,7 +1371,7 @@ function scheduler(task) {
1371
1371
``` javascript
1372
1372
let steps = [step1Func, step2Func, step3Func];
1373
1373
1374
- function * iterateSteps (steps ){
1374
+ function * iterateSteps (steps ){
1375
1375
for (var i= 0 ; i< steps .length ; i++ ){
1376
1376
var step = steps[i];
1377
1377
yield step ();
@@ -1467,7 +1467,7 @@ gen.next().done // true
1467
1467
Generator 可以看作是数据结构,更确切地说,可以看作是一个数组结构,因为 Generator 函数可以返回一系列的值,这意味着它可以对任意表达式,提供类似数组的接口。
1468
1468
1469
1469
``` javascript
1470
- function * doStuff () {
1470
+ function * doStuff () {
1471
1471
yield fs .readFile .bind (null , ' hello.txt' );
1472
1472
yield fs .readFile .bind (null , ' world.txt' );
1473
1473
yield fs .readFile .bind (null , ' and-such.txt' );
You can’t perform that action at this time.
0 commit comments