diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:40:17 -0500 |
| commit | 33058f2b292b3a581333bdfb21b8f671898c5060 (patch) | |
| tree | 51c3e392513ec574331b2d3f85c394445ea803c6 /files/zh-cn/web/javascript/reference/global_objects/generator | |
| parent | 8b66d724f7caf0157093fb09cfec8fbd0c6ad50a (diff) | |
| download | translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.gz translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.tar.bz2 translated-content-33058f2b292b3a581333bdfb21b8f671898c5060.zip | |
initial commit
Diffstat (limited to 'files/zh-cn/web/javascript/reference/global_objects/generator')
4 files changed, 548 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> diff --git a/files/zh-cn/web/javascript/reference/global_objects/generator/next/index.html b/files/zh-cn/web/javascript/reference/global_objects/generator/next/index.html new file mode 100644 index 0000000000..96ad60c35c --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/generator/next/index.html @@ -0,0 +1,165 @@ +--- +title: Generator.prototype.next() +slug: Web/JavaScript/Reference/Global_Objects/Generator/next +tags: + - ECMAScript 2015 + - Generator + - JavaScript + - 原型 + - 参考 + - 方法 + - 生成器 +translation_of: Web/JavaScript/Reference/Global_Objects/Generator/next +--- +<div>{{JSRef}}</div> + +<p><code><strong>next</strong></code><strong><code>()</code></strong> 方法返回一个包含属性 <code>done</code> 和 <code>value</code> 的对象。该方法也可以通过接受一个参数用以向生成器传值。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><var>gen</var>.next(value)</code></pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>value</code></dt> + <dd>向生成器传递的值.</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>返回的<code><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object">对象</a></code>包含两个属性:</p> + +<ul> + <li><code>done</code> (布尔类型) + + <ul> + <li>如果迭代器超过迭代序列的末尾,则值为 <code>true</code>。 在这种情况下,<code>value</code>可选地指定迭代器的返回值。</li> + <li>如果迭代器能够生成序列中的下一个值,则值为<code>false</code>。 这相当于没有完全指定<code>done</code>属性。</li> + </ul> + </li> + <li><code>value</code> - 迭代器返回的任意的Javascript值。当 <code>done</code> 的值为 <code>true </code>时可以忽略该值。</li> +</ul> + +<h2 id="示例">示例</h2> + +<h3 id="Example_Using_test" name="Example:_Using_test">使用 <code>next()方法</code></h3> + +<p>下面的例子展示了一个简单的生成器, 以及调用 <code>next</code> 后方法的返回值:</p> + +<pre class="brush: js">function* gen() { + yield 1; + yield 2; + yield 3; +} + +var g = gen(); // "Generator { }" +g.next(); // "Object { value: 1, done: false }" +g.next(); // "Object { value: 2, done: false }" +g.next(); // "Object { value: 3, done: false }" +g.next(); // "Object { value: undefined, done: true }" +</pre> + +<h3 id="向生成器传值">向生成器传值</h3> + +<p>在此示例中,使用值调用<code>next</code>。 请注意,第一次调用没有记录任何内容,因为生成器最初没有产生任何结果。</p> + +<pre><code>function* gen() { + while(true) { + var value = yield null; + console.log(value); + } +} + +var g = gen(); +g.next(1); +// "{ value: null, done: false }" +g.next(2); +// 2 +// "{ value: null, done: false }"</code> +</pre> + +<h2 id="Specifications" name="Specifications">规范</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('ES6', '#sec-generator.prototype.next', 'Generator.prototype.next')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>初始定义</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-generator.prototype.next', 'Generator.prototype.next')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td>草案</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div> +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Edge</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatVersionUnknown}}</td> + <td>13</td> + <td>{{CompatGeckoDesktop(26)}}</td> + <td>{{CompatNo}}</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatSafari(10)}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>5.1</td> + <td>{{CompatVersionUnknown}}</td> + <td>{{CompatGeckoMobile(26)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>10</td> + </tr> + </tbody> +</table> +</div> +</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function*">function*</a></code></li> + <li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators">Iterators and generators</a></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/generator/return/index.html b/files/zh-cn/web/javascript/reference/global_objects/generator/return/index.html new file mode 100644 index 0000000000..9c5ed4bb99 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/generator/return/index.html @@ -0,0 +1,99 @@ +--- +title: Generator.prototype.return() +slug: Web/JavaScript/Reference/Global_Objects/Generator/return +tags: + - ECMAScript6 + - JavaScript + - 原型 + - 参考 + - 方法 + - 生成器 +translation_of: Web/JavaScript/Reference/Global_Objects/Generator/return +--- +<div>{{JSRef}}</div> + +<p><code><strong>return</strong></code><strong><code>()</code></strong> 方法返回给定的值并结束生成器。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><var>gen</var>.return(value)</code></pre> + +<h3 id="Parameters" name="Parameters">参数</h3> + +<dl> + <dt><code>value</code></dt> + <dd>需要返回的值</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>返回该函数参数中给定的值.</p> + +<h2 id="示例">示例</h2> + +<h3 id="Example_Using_test" name="Example:_Using_test">使用 <code>return()</code></h3> + +<p>以下例子展示了一个简单的生成器和 <code>return</code> 方法的使用.</p> + +<pre class="brush: js">function* gen() { + yield 1; + yield 2; + yield 3; +} + +var g = gen(); + +g.next(); // { value: 1, done: false } +g.return("foo"); // { value: "foo", done: true } +g.next(); // { value: undefined, done: true } +</pre> + +<p>如果对已经处于“完成”状态的生成器调用<code>return(value)</code>,则生成器将保持在“完成”状态。如果没有提供参数,则返回对象的<code>value</code>属性与示例最后的<code>.next()</code>方法相同。如果提供了参数,则参数将被设置为返回对象的<code>value</code>属性的值。</p> + +<pre class="brush: js">function* gen() { + yield 1; + yield 2; + yield 3; +} + +var g = gen(); +g.next(); // { value: 1, done: false } +g.next(); // { value: 2, done: false } +g.next(); // { value: 3, done: false } +g.next(); // { value: undefined, done: true } +g.return(); // { value: undefined, done: true } +g.return(1); // { value: 1, done: true }</pre> + +<h2 id="Specifications" name="Specifications">规范</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('ES6', '#sec-generator.prototype.return', 'Generator.prototype.return')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-generator.prototype.return', 'Generator.prototype.return')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td></td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + + + +<p>{{Compat("javascript.builtins.Generator.return")}}</p> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function*">function*</a></code></li> +</ul> diff --git a/files/zh-cn/web/javascript/reference/global_objects/generator/throw/index.html b/files/zh-cn/web/javascript/reference/global_objects/generator/throw/index.html new file mode 100644 index 0000000000..13138d5484 --- /dev/null +++ b/files/zh-cn/web/javascript/reference/global_objects/generator/throw/index.html @@ -0,0 +1,142 @@ +--- +title: Generator.prototype.throw() +slug: Web/JavaScript/Reference/Global_Objects/Generator/throw +tags: + - ECMAScript6 + - JavaScript + - 参考 + - 属性 + - 方法 + - 生成器 +translation_of: Web/JavaScript/Reference/Global_Objects/Generator/throw +--- +<div>{{JSRef}}</div> + +<p><code><strong>throw</strong></code><strong><code>()</code></strong> 方法用来向生成器抛出异常,并恢复生成器的执行,返回带有 <code>done</code> 及 <code>value</code> 两个属性的对象。</p> + +<h2 id="Syntax" name="Syntax">语法</h2> + +<pre class="syntaxbox"><code><var>gen</var>.throw(exception)</code></pre> + +<h3 id="参数">参数</h3> + +<dl> + <dt><code>exception</code></dt> + <dd>用于抛出的异常。 使用 {{jsxref("Error")}} 的实例对调试非常有帮助.</dd> +</dl> + +<h3 id="返回值">返回值</h3> + +<p>带有两个属性的{{jsxref("Object", "对象")}}:</p> + +<ul> + <li><code>done</code> (boolean) + + <ul> + <li>如果迭代器已经返回了迭代序列的末尾,则值为 <code>true</code>。在这种情况下,<code>可以</code>指定迭代器 <code>value </code>的返回值。 </li> + <li>如果迭代能够继续生产在序列中的下一个值,则值为 <code>false</code>。 这相当与不指定 done 属性的值。</li> + </ul> + </li> + <li><code>value</code> - 迭代器返回的任何 JavaScript 值。当 done 是 true 的时候可以省略。</li> +</ul> + +<h2 id="示例">示例</h2> + +<h3 id="Example:_Using_test" name="Example:_Using_test">使用 <code>throw()</code></h3> + +<p>下面的例子展示了一个简单的生成器并使用 <span style="font-family: courier,andale mono,monospace;">throw方法</span>向该生成器抛出一个异常,该异常通常可以通过 <code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/try...catch">try...catch</a></code> 块进行捕获.</p> + +<pre class="brush: js">function* gen() { + while(true) { + try { + yield 42; + } catch(e) { + console.log("Error caught!"); + } + } +} + +var g = gen(); +g.next(); // { value: 42, done: false } +g.throw(new Error("Something went wrong")); // "Error caught!" +</pre> + +<h2 id="Specifications" name="Specifications">规范</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('ES6', '#sec-generator.prototype.throw', 'Generator.prototype.throw')}}</td> + <td>{{Spec2('ES6')}}</td> + <td>Initial definition.</td> + </tr> + <tr> + <td>{{SpecName('ESDraft', '#sec-generator.prototype.throw', 'Generator.prototype.throw')}}</td> + <td>{{Spec2('ESDraft')}}</td> + <td> </td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2> + +<div>{{CompatibilityTable}}</div> + +<div id="compat-desktop"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Chrome</th> + <th>Firefox (Gecko)</th> + <th>Internet Explorer</th> + <th>Opera</th> + <th>Safari</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoDesktop(26)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<div id="compat-mobile"> +<table class="compat-table"> + <tbody> + <tr> + <th>Feature</th> + <th>Android</th> + <th>Chrome for Android</th> + <th>Firefox Mobile (Gecko)</th> + <th>IE Mobile</th> + <th>Opera Mobile</th> + <th>Safari Mobile</th> + </tr> + <tr> + <td>Basic support</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatGeckoMobile(26)}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + <td>{{CompatUnknown}}</td> + </tr> + </tbody> +</table> +</div> + +<h2 id="See_also" name="See_also">相关链接</h2> + +<ul> + <li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function*">function*</a></code></li> +</ul> |
