aboutsummaryrefslogtreecommitdiff
path: root/files/vi/web/javascript/reference/global_objects/generator/index.html
blob: a1ed3ecf07b1216eb723350f82489e9d6b400e2e (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
---
title: Generator
slug: Web/JavaScript/Reference/Global_Objects/Generator
translation_of: Web/JavaScript/Reference/Global_Objects/Generator
---
<div>{{JSRef}}</div>

<p><code><strong>Generator</strong></code> là một object return bởi một {{jsxref("Statements/function*", "generator function", "", 1)}}, nó phù hợp với cả <a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterable_protocol">iterable protocol</a> và <a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol">iterator protocol</a>.</p>

<h2 id="Cú_pháp">Cú pháp</h2>

<pre class="syntaxbox">function* gen() {
  yield 1;
  yield 2;
  yield 3;
}

var g = gen(); // "Generator { }"</pre>

<h2 id="Phương_thức">Phương thức</h2>

<dl>
 <dt>{{jsxref("Generator.prototype.next()")}}</dt>
 <dd>Trả về giá trị yielded, được khai báo qua câu lệnh {{jsxref("Operators/yield", "yield")}}.</dd>
 <dt>{{jsxref("Generator.prototype.return()")}}</dt>
 <dd>Trả về giá trị và kết thúc generator.</dd>
 <dt>{{jsxref("Generator.prototype.throw()")}}</dt>
 <dd>Quăng lỗi vào generator (đồng thời kết thúc generator, trừ khi được bắt lại trong generator đó).</dd>
</dl>

<h2 id="Ví_dụ">Ví dụ</h2>

<h3 id="Một_vòng_lặp_vô_hạn">Một vòng lặp vô hạn</h3>

<pre class="brush: js">function* idMaker() {
    var index = 0;
    while(true)
        yield index++;
}

var gen = idMaker(); // "Generator { }"

console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
// ...</pre>

<h2 id="Generator_object_cũ">Generator object cũ</h2>

<p>Firefox (SpiderMonkey) đã hiện thực phiên bản generators đầu tiên trong <a href="/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7">JavaScript 1.7</a>, lúc đó dấu sao (*) trong khai báo không bắt buộc (bạn chỉ cần dùng từ khóa <code>yield</code> bên trong hàm). Tuy nhiên, kiểu viết này đã không còn được hổ trợ từ Firefox 58 (released ngày 23, tháng 1, 2018) ({{bug(1083482)}}).</p>

<h3 id="Các_phương_thức_generator_cũ">Các phương thức generator cũ</h3>

<dl>
 <dt><code>Generator.prototype.next() </code>{{non-standard_inline}}</dt>
 <dd>Returns a value yielded by the {{jsxref("Operators/yield", "yield")}} expression. This corresponds to <code>next()</code> in the ES2015 generator object.</dd>
 <dt><code>Generator.prototype.close()</code> {{non-standard_inline}}</dt>
 <dd>Closes the generator, so that when calling <code>next()</code> an {{jsxref("StopIteration")}} error will be thrown. This corresponds to the <code>return()</code> method in the ES2015 generator object.</dd>
 <dt><code>Generator.prototype.send()</code> {{non-standard_inline}}</dt>
 <dd>Used to send a value to a generator. The value is returned from the {{jsxref("Operators/yield", "yield")}} expression, and returns a value yielded by the next {{jsxref("Operators/yield", "yield")}} expression. <code>send(x)</code> corresponds to <code>next(x)</code> in the ES2015 generator object.</dd>
 <dt><strong><code>Generator.</code></strong><code>prototype.</code><strong><code>throw()</code> </strong> {{non-standard_inline}}</dt>
 <dd>Throws an error to a generator. This corresponds to the <code>throw()</code> method in the ES2015 generator object.</dd>
</dl>

<h3 id="Legacy_generator_example">Legacy generator example</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="Specifications">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('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="Trình_duyệt_hổ_trợ">Trình duyệt hổ trợ</h2>



<p>{{Compat("javascript.builtins.Generator")}}</p>

<h2 id="Xem_thêm">Xem thêm</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>