aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/generator/index.html
blob: f7e4fc037bec8a893f6ab8e30cc4a6427e342557 (plain)
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
---
title: Generator
slug: Web/JavaScript/Reference/Global_Objects/Generator
tags:
  - ECMAScript 2015
  - Generator
  - JavaScript
  - Legacy Generator
  - Legacy Iterator
  - 参考
  - 生成器
translation_of: Web/JavaScript/Reference/Global_Objects/Generator
---
<div>{{JSRef}} </div>

<p><strong>生成器</strong>对象是由一个 {{jsxref("Statements/function*", "generator function", "", 1)}} 返回的,并且它符合<a href="/zh-CN/docs/Web/JavaScript/Reference/Iteration_protocols#iterable">可迭代协议</a><a href="/zh-CN/docs/Web/JavaScript/Reference/Iteration_protocols#iterator">迭代器协议</a></p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox">function* gen() {
  yield 1;
  yield 2;
  yield 3;
}

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

<h2 id="方法">方法</h2>

<dl>
 <dt>{{jsxref("Generator.prototype.next()")}}</dt>
 <dd>返回一个由 {{jsxref("Operators/yield", "yield")}}表达式生成的值。</dd>
 <dt>{{jsxref("Generator.prototype.return()")}}</dt>
 <dd>返回给定的值并结束生成器。</dd>
 <dt>{{jsxref("Generator.prototype.throw()")}}</dt>
 <dd>向生成器抛出一个错误。</dd>
</dl>

<h2 id="示例">示例</h2>

<h3 id="一个无限迭代器">一个无限迭代器</h3>

<pre class="brush: js">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
// ...</pre>

<h2 id="传统的生成器对象">传统的生成器对象</h2>

<p>Firefox (SpiderMonkey) 在 <a href="/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7">JavaScript 1.7</a> 中也实现了一个较早版本的生成器,其中函数声明中的星号(*)不是必需的 (只需在函数体中使用<code style="font-style: normal;">yield</code> 关键字)。但是,旧式生成器已弃用。不要使用它们;他们将被删除  ({{bug(1083482)}})。</p>

<h3 id="传统的生成器方法">传统的生成器方法</h3>

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

<h3 id="旧生成器对象示例">旧生成器对象示例</h3>

<pre class="brush: js">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)
</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('ES2015', '#sec-generator-objects', 'Generator objects')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#sec-generator-objects', 'Generator objects')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("javascript.builtins.Generator")}}</p>

<h2 id="See_also" name="See_also">相关链接</h2>

<h3 id="Legacy_generators">Legacy generators</h3>

<ul>
 <li>{{jsxref("Statements/Legacy_generator_function", "The legacy generator function", "", 1)}}</li>
 <li>{{jsxref("Operators/Legacy_generator_function", "The legacy generator function expression", "", 1)}}</li>
 <li>{{jsxref("StopIteration")}}</li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Deprecated_and_obsolete_features/The_legacy_Iterator_protocol">The legacy Iterator protocol</a></li>
</ul>

<h3 id="ES2015_generators">ES2015 generators</h3>

<ul>
 <li>{{jsxref("Functions", "Functions", "", 1)}}</li>
 <li>{{jsxref("Statements/function", "function")}}</li>
 <li>{{jsxref("Operators/function", "function expression")}}</li>
 <li>{{jsxref("Function")}}</li>
 <li>{{jsxref("Statements/function*", "function*")}}</li>
 <li>{{jsxref("Operators/function*", "function* expression")}}</li>
 <li>{{jsxref("GeneratorFunction")}}</li>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">The Iterator protocol</a></li>
</ul>