aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/javascript/reference/global_objects/generator/next/index.html
blob: 862cf598be2fae644fed278c80ac537c541a39d5 (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
---
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">语法</h2>

<pre class="syntaxbox"><code><var>gen</var>.next(value)</code></pre>

<h3 id="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">使用 <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">规范</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="浏览器兼容性">浏览器兼容性</h2>

{{Compat}}

<h2 id="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>