js生成器中next的使用

说明

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

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

实例

constcanBeStoppedCounter=(function*(){
letc=0;
letshouldBreak=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教程

原文来自:https://www.py.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享