【es6】Generator 函数

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中

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