🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# generator yield既可以传参,也可以返回 ## 1.1传参 ~~~ function *show(){ alert("a"); let a = yield; alert("b"); alert(a); } let gen = show(); gen.next(); gen.next(5); ~~~ ## 1.2返回值 ~~~ function *show(){ alert("a"); yield 12; alert("b"); } let gen = show(); let obj1 = gen.next(); let obj2 = gen.next(); console.log(obj1); //{value: 12, done: false} console.log(obj2); //{value: undefined, done: true} ~~~ ~~~ //最后一个值靠return function *show(){ alert("a"); yield 12; alert("b"); return 22; } let gen = show(); let obj1 = gen.next(); let obj2 = gen.next(); console.log(obj1); //{value: 12, done: false} console.log(obj2); //{value: 22, done: true} ~~~