yield* 表达式用于委托给另一个generator 或可迭代对象。

语法

 yield* [[expression]];
expression
返回一个可迭代对象的表达式。

描述

yield* 表达式迭代操作数,并产生它返回的每个值。

yield* 表达式本身的值是当迭代器关闭时返回的值(即donetrue时)。

示例

委托给其他生成器

以下代码中,g1() yield 出去的每个值都会在 g2() 的 next() 方法中返回,就像那些 yield 语句是写在 g2() 里一样。

function* g1() {
  yield 2;
  yield 3;
  yield 4;
}

function* g2() {
  yield 1;
  yield* g1();
  yield 5;
}

var iterator = g2();

console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: 4, done: false }
console.log(iterator.next()); // { value: 5, done: false }
console.log(iterator.next()); // { value: undefined, done: true }

委托给其他可迭代对象

除了生成器对象这一种可迭代对象,yield* 还可以 yield 其它任意的可迭代对象,比如说数组、字符串、arguments 对象等等。

function* g3() {
  yield* [1, 2];
  yield* "34";
  yield* arguments;
}

var iterator = g3(5, 6);

console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: "3", done: false }
console.log(iterator.next()); // { value: "4", done: false }
console.log(iterator.next()); // { value: 5, done: false }
console.log(iterator.next()); // { value: 6, done: false }
console.log(iterator.next()); // { value: undefined, done: true }

yield* 表达式的值

yield* 是一个表达式,不是语句,所以它会有自己的值。

function* g4() {
  yield* [1, 2, 3];
  return "foo";
}

var result;

function* g5() {
  result = yield* g4();
}

var iterator = g5();

console.log(iterator.next()); // { value: 1, done: false }
console.log(iterator.next()); // { value: 2, done: false }
console.log(iterator.next()); // { value: 3, done: false }
console.log(iterator.next()); // { value: undefined, done: true }, 
                              // 此时 g4() 返回了 { value: "foo", done: true }

console.log(result);          // "foo"

规范

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

浏览器兼容

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidEdge MobileFirefox for AndroidOpera for AndroidiOS SafariSamsung InternetNode.js
Basic supportChrome Full support YesEdge Full support YesFirefox Full support 27
Notes
Full support 27
Notes
Notes Starting with Firefox 33, the parsing of the yield expression has been updated to conform with the ES2015 specification.
IE No support NoOpera Full support YesSafari Full support 10WebView Android Full support YesChrome Android Full support YesEdge Mobile Full support YesFirefox Android Full support 27
Notes
Full support 27
Notes
Notes Starting with Firefox 33, the parsing of the yield expression has been updated to conform with the ES2015 specification.
Opera 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
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.

Firefox 特别提示

  • 从 Gecko 33 (Firefox 33 / Thunderbird 33 / SeaMonkey 2.30) 开始,yield表达式的解析已被更新以符合ES2015规范(bug 981599):
    • 现在有行结束符限制。在 yield* 之间不允许有换行符。如下代码会抛出SyntaxError
      function* foo() {
        yield
        *[];
      }

相关链接

文档标签和贡献者

此页面的贡献者: zhangchen, xgqfrms-GitHub, ccn1010, ziyunfei, Liyunsheng
最后编辑者: zhangchen,