ECMAScript 6 Generator
定义生成器函数
const GeneratorFunction = Reflect.getPrototypeOf(function* (){}).constructor;const GeneratorFunction = Reflect.getPrototypeOf(function* (){}).constructor;
let g = new GeneratorFunction('a', 'yield a * 2');
let gObj = g(10);
gObj.next(); // {value: 20, done: false}function* gen (x) {
yield 1;
return x;
}
let genObj = gen(10);
genObj.next(); // 执行 yield 1,返回 {value: 1, done: false}
genObj.next(); // 执行 return x,返回 {value: 10, done: true}
genObj.next(); // 执行完毕 {value: undefined, done: true}调用生成器函数
迭代器对象的方法
最后更新于