1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
---
title: yield*
slug: Web/JavaScript/Reference/Operators/yield*
tags:
- ECMAScript 2015
- Generators
- Iterable
- Iterator
- JavaScript
- Operator
- Reference
translation_of: Web/JavaScript/Reference/Operators/yield*
---
<div>{{jsSidebar("Operators")}}</div>
<p> <strong><code>yield*</code> 表达式</strong>用于委托给另一个{{jsxref("Statements/function*", "generator")}} 或可迭代对象。</p>
<h2 id="Syntax">语法</h2>
<pre class="syntaxbox"> yield* [[expression]];</pre>
<dl>
<dt><code>expression</code></dt>
<dd>返回一个可迭代对象的表达式。</dd>
</dl>
<h2 id="描述">描述</h2>
<p><code>yield*</code> 表达式迭代操作数,并产生它返回的每个值。</p>
<p><code>yield*</code> 表达式本身的值是当迭代器关闭时返回的值(即<code>done</code>为<code>true</code>时)。</p>
<h2 id="Examples">示例</h2>
<h3 id="委托给其他生成器">委托给其他生成器</h3>
<p>以下代码中,<code>g1()</code> <code>yield</code> 出去的每个值都会在 <code>g2()</code> 的 <code>next()</code> 方法中返回,就像那些 <code>yield</code> 语句是写在 <code>g2()</code> 里一样。</p>
<pre class="brush: js">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 }
</pre>
<h3 id="委托给其他可迭代对象">委托给其他可迭代对象</h3>
<p>除了生成器对象这一种可迭代对象,<code>yield*</code> 还可以 <code>yield</code> 其它任意的可迭代对象,比如说数组、字符串、<code>arguments</code> 对象等等。</p>
<pre class="brush: js">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 }
</pre>
<h3 id="yield*_表达式的值"><code>yield*</code> 表达式的值</h3>
<p><code>yield*</code> 是一个表达式,不是语句,所以它会有自己的值。</p>
<pre class="brush: js">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"
</pre>
<h2 id="Specifications">规范</h2>
<table class="standard-table">
<thead>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{SpecName('ES2015', '#', 'Yield')}}</td>
<td>{{Spec2('ES2015')}}</td>
<td>Initial definition.</td>
</tr>
<tr>
<td>{{SpecName('ESDraft', '#', 'Yield')}}</td>
<td>{{Spec2('ESDraft')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility">浏览器兼容</h2>
<p>{{Compat("javascript.operators.yield_star")}}</p>
<h2 id="Firefox_特别提示">Firefox 特别提示</h2>
<ul>
<li>从 Gecko 33 {{geckoRelease(33)}} 开始,<code>yield</code>表达式的解析已被更新以符合ES2015规范({{bug(981599)}}):
<ul>
<li>现在有行结束符限制。在 <code>yield</code> 和 <code>*</code> 之间不允许有换行符。如下代码会抛出{{jsxref("SyntaxError")}}:
<pre class="brush: js">function* foo() {
yield
*[];
}</pre>
</li>
</ul>
</li>
</ul>
<h2 id="See_also">相关链接</h2>
<ul>
<li><a href="/zh-CN/docs/Web/JavaScript/Guide/The_Iterator_protocol">迭代器协议</a></li>
<li>{{jsxref("Statements/function*", "function*")}}</li>
<li>{{jsxref("Operators/function*", "function* expression")}}</li>
<li>{{jsxref("Operators/yield", "yield")}}</li>
</ul>
|