码迷,mamicode.com
首页 > 其他好文 > 详细

ES6:Generator函数(1)

时间:2018-03-08 12:03:20      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:fun   sum   ext   解决方案   generator   eva   blog   span   fine   

Generator函数是ES6提供的一种异步编程解决方案。它会返回一个遍历器对象

function* helloWorldGenerator(){

      yield “hello”;

      yield “world”;

      return “ending”;

}
var hw = helloWorldGenerator();

hw.next();  //{value:”hello”,done:false}

hw.next(); //{value:”world”,done:false}

hw.next(); //{value:”ending”,done:true}

hw.next(); //{value:undefined,done:true}

*yield只能用在Generator函数中,用在普通函数中会报错

*yield表达式在使用在另一个表达式中时必须加括号console.log((yield))

用途:手动“惰性求职”(Lazy Evalution)

function* sumGenerator(){

    yield 123+345;

}

 

ES6:Generator函数(1)

标签:fun   sum   ext   解决方案   generator   eva   blog   span   fine   

原文地址:https://www.cnblogs.com/jinyuGu/p/8527146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!