diff options
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/generator/index.html')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/generator/index.html | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/files/zh-cn/web/javascript/reference/global_objects/generator/index.html b/files/zh-cn/web/javascript/reference/global_objects/generator/index.html new file mode 100644 index 0000000000..612024ca88 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/generator/index.html @@ -0,0 +1,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 生成器对象的<span style="font-family: courier,andale mono,monospace;">next()方法对应</span>.</dd> + <dt><code>Generator.prototype.close()</code> {{non-standard_inline}}</dt> + <dd>关闭生成器,因此执行该函数后调用<code>next()函数时将会抛出</code> {{jsxref("StopIteration")}} 错误. 与ES2015 生成器对象的<span style="font-family: courier,andale mono,monospace;">return()方法对应</span>..</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 生成器对象的<span style="font-family: courier,andale mono,monospace;">throw()方法对应</span>.</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> |