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/next | |
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/next')
-rw-r--r-- | files/zh-cn/web/javascript/reference/global_objects/generator/next/index.html | 165 |
1 files changed, 165 insertions, 0 deletions
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> |