Generator

 

生成器对象是由一个 generator function 返回的,并且它符合可迭代协议迭代器协议

语法

function* gen() { 
  yield 1;
  yield 2;
  yield 3;
}

let g = gen(); 
// "Generator { }"

方法

Generator.prototype.next()
返回一个由 yield表达式生成的值。
Generator.prototype.return()
返回给定的值并结束生成器。
Generator.prototype.throw()
向生成器抛出一个错误。

示例

一个无限迭代器

function* idMaker(){
    let index = 0;
    while(true)
        yield index++;
}

let gen = idMaker(); // "Generator { }"

console.log(gen.next().value); 
// 0
console.log(gen.next().value); 
// 1
console.log(gen.next().value); 
// 2
// ...

传统的生成器对象

Firefox (SpiderMonkey) 在 JavaScript 1.7 中也实现了一个较早版本的生成器,其中函数声明中的星号(*)不是必需的 (只需在函数体中使用yield 关键字)。但是,旧式生成器已弃用。不要使用它们;他们将被删除  (bug 1083482)。

传统的生成器方法

Generator.prototype.next()
返回 yield 表达式产生的值. 与ES2015 生成器对象的next()方法对应.
Generator.prototype.close()
关闭生成器,因此执行该函数后调用next()函数时将会抛出 StopIteration 错误. 与ES2015 生成器对象的return()方法对应..
Generator.prototype.send()
用于将值发送到生成器。 该值由 yield 表达式返回, 并且返回下一个 yield 表达式产生的值. send(x) 对应于ES2015生成器对象中的 next(x)
Generator.prototype.throw()
向生成器抛出错误. 与ES2015 生成器对象的throw()方法对应.

旧生成器对象示例

function fibonacci() {
  var a = yield 1;
  yield a * 2;
}

var it = fibonacci();
console.log(it);          // "Generator {  }"
console.log(it.next());   // 1
console.log(it.send(10)); // 20
console.log(it.close());  // undefined
console.log(it.next());   // throws StopIteration (as the generator is now closed)

规范

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
Generator objects
Standard Initial definition.
ECMAScript Latest Draft (ECMA-262)
Generator objects
Draft

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
GeneratorChrome Full support 39Edge Full support 13Firefox Full support 26IE No support NoOpera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 26Opera Android Full support YesSafari iOS Full support 10Samsung Internet Android Full support Yesnodejs Full support 4.0.0
Full support 4.0.0
Full support 0.12
Disabled
Disabled From version 0.12: this feature is behind the --harmony runtime flag.
nextChrome Full support 39Edge Full support 13Firefox Full support 26IE No support NoOpera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 26Opera Android Full support YesSafari iOS Full support 10Samsung Internet Android Full support Yesnodejs Full support Yes
returnChrome Full support 50Edge Full support 13Firefox Full support 38IE No support NoOpera Full support 37Safari Full support 10WebView Android Full support 50Chrome Android Full support 50Firefox Android Full support 38Opera Android Full support YesSafari iOS Full support 10Samsung Internet Android Full support 5.0nodejs Full support 6.0.0
throwChrome Full support 39Edge Full support 13Firefox Full support 26IE No support NoOpera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesFirefox Android Full support 26Opera Android Full support YesSafari iOS Full support 10Samsung Internet Android Full support Yesnodejs Full support 4.0.0
Full support 4.0.0
Full support 0.12
Disabled
Disabled From version 0.12: this feature is behind the --harmony runtime flag.

Legend

Full support  
Full support
No support  
No support
User must explicitly enable this feature.
User must explicitly enable this feature.

相关链接

Legacy generators

ES2015 generators