首页 > 其他 > 详细

【es6】Generator 函数

时间:2018-03-24 18:53:16      阅读:193      评论:0      收藏:0      [点我收藏+]

1. 基本概念

状态机,封装了多个内部状态

2. 应用

返回一个遍历器对象。

3. 代码形式

function* helloWorldGenertor() {
    yield ‘hello‘;
    yield ‘world‘;
    return ‘ending‘;
}
var hw = helloWorldGenertor();

调用

hw.next()

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

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

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

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

4.扩展

① yield与return的相似和不同

yield只能用在generator中

【es6】Generator 函数

原文:https://www.cnblogs.com/teemor/p/8640692.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!