mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-11 00:54:06 +08:00
164 lines
6.4 KiB
HTML
164 lines
6.4 KiB
HTML
<article id="wikiArticle">
|
||
<div></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><code class="language-javascript"><code><var>gen</var>.next(value)</code></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><code class="language-javascript">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 }"
|
||
</code></pre>
|
||
<h3 id="向生成器传值">向生成器传值</h3>
|
||
<p>在该示例中,调用 <code>next</code> 方法并传入了参数,请注意,首次调用 <code style="font-style: normal;">next</code> 方法时参数0被丢弃.</p>
|
||
<pre><code class="language-javascript">/** gen函数运行解析:
|
||
* i=0 时传入参数(0),并将参数0赋给上一句yield的返回赋值,由于没有上一句yield语句,这步被忽略
|
||
* 执行var val =100,然后执行yield val,此时g.next(i)返回{ value: 100, done: false }
|
||
* 然后console.log(i,g.next(i).value),打印出0 100
|
||
*
|
||
* i=1 时传入参数(1),并将参数1赋给上一句yield的返回赋值,即(val = 1)
|
||
* 然后执行console.log(val),打印出1。
|
||
* 接着进入第二次while循环,调用yield val,此时g.next(i)返回{ value: 1, done: false }
|
||
* 然后console.log(i,g.next(i).value),打印出1 1
|
||
*
|
||
* i=2 ....(省略)
|
||
*/
|
||
function* gen() {
|
||
var val =100;
|
||
while(true) {
|
||
val = yield val;
|
||
console.log(val);
|
||
}
|
||
}
|
||
|
||
var g = gen();
|
||
for(let i =0;i<5;i++){
|
||
console.log(i,g.next(i).value);
|
||
}
|
||
|
||
/* 返回:
|
||
0 100
|
||
1
|
||
1 1
|
||
2
|
||
2 2
|
||
3
|
||
3 3
|
||
4
|
||
4 4
|
||
*/</code></pre>
|
||
<p><img alt="向生成器传值的示例数据流向解析" src="https://mdn.mozillademos.org/files/15779/%E5%90%91%E7%94%9F%E6%88%90%E5%99%A8%E4%BC%A0%E5%80%BC.png"/></p>
|
||
<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><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-generator.prototype.next" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Generator.prototype.next</small></a></td>
|
||
<td><span class="spec-Standard">Standard</span></td>
|
||
<td>初始定义</td>
|
||
</tr>
|
||
<tr>
|
||
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-generator.prototype.next" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Generator.prototype.next</small></a></td>
|
||
<td><span class="spec-Draft">Draft</span></td>
|
||
<td>草案</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>
|
||
<div><div class="blockIndicator warning"><strong><a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">We're converting our compatibility data into a machine-readable JSON format</a></strong>.
|
||
This compatibility table still uses the old format,
|
||
because we haven't yet converted the data it contains.
|
||
<strong><a class="new" href="/zh-CN/docs/MDN/Contribute/Structures/Compatibility_tables" rel="nofollow">Find out how you can help!</a></strong></div>
|
||
<div class="htab">
|
||
<a id="AutoCompatibilityTable" name="AutoCompatibilityTable"></a>
|
||
<ul>
|
||
<li class="selected"><a>Desktop</a></li>
|
||
<li><a>Mobile</a></li>
|
||
</ul>
|
||
</div></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><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td>13</td>
|
||
<td><a href="/en-US/Firefox/Releases/26" title="Released on 2013-12-10.">26</a> (26)</td>
|
||
<td><span style="color: #f00;">未实现</span></td>
|
||
<td><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td>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><span style="color: #888;" title="Please update this with the earliest version of support.">(Yes)</span></td>
|
||
<td>26.0 (26)</td>
|
||
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></td>
|
||
<td><span style="color: rgb(255, 153, 0);" title="Compatibility unknown; please update this.">?</span></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>
|
||
</article> |