好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

js生成器中next的使用

说明

1、生成器函数的外部可以向next方法传达参数,该参数作为上一个yield表现的返回值。

2、如果不传递参数,yield表达式返回undefined。

实例

const canBeStoppedCounter = (function* () {
  let c = 0;
  let shouldBreak = false;
  while (true) {
    shouldBreak = yield ++c;
    console.log(shouldBreak);
    if (shouldBreak) return;
  }
};
 
canBeStoppedCounter.next();
// { value: 1, done: false }
 
canBeStoppedCounter.next();
// undefined,第一次执行 yield 表达式的返回值
// { value: 2, done: false }
 
canBeStoppedCounter.next(true);
// true,第二次执行 yield 表达式的返回值
// { value: undefined, done: true }

以上就是js生成器中next的使用,希望对大家有所帮助。 更多js学习指路: js教程

推荐操作环境:windows7系统、jquery3.2.1版本,DELL G3电脑。

查看更多关于js生成器中next的使用的详细内容...

  阅读:14次