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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
---
title: Generator
slug: Web/JavaScript/Reference/Global_Objects/Generator
tags:
- ECMAScript 2015
- Generator
- JavaScript
- Legacy Generator
- Legacy Iterator
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Generator
---
<div>{{JSRef}}</div>
<p><code><strong>Generator</strong></code> 객체는 {{jsxref("Statements/function*", "generator function", "", 1)}} 으로부터 반환된 값이며 <a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">반복자와 반복자 프로토콜</a>을 준수합니다.</p>
<h2 id="문법">문법</h2>
<pre class="syntaxbox">function* gen() {
yield 1;
yield 2;
yield 3;
}
var g = gen(); // "Generator { }"</pre>
<h2 id="메서드">메서드</h2>
<dl>
<dt>{{jsxref("Generator.prototype.next()")}}</dt>
<dd>{{jsxref("Operators/yield", "yield")}} 표현을 통해 yield된 값을 반환합니다.</dd>
<dt>{{jsxref("Generator.prototype.return()")}}</dt>
<dd>주어진 값을 반환하고 생성기를 종료합니다.</dd>
<dt>{{jsxref("Generator.prototype.throw()")}}</dt>
<dd>생성기로 에러를 throw합니다.</dd>
</dl>
<h2 id="예시">예시</h2>
<h3 id="무한_반복자">무한 반복자</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_객체">오래된 Generator 객체</h2>
<p>Firefox (SpiderMonkey)는 <a href="/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7">JavaScript 1.7</a>에서 이전 버전의 생성기도 구현 했습니다.이 버전에서는 함수 선언에서 별표 (*)가 필요하지 않습니다 (함수 본문에서 <code>yield</code> 키워드 만 사용). 그러나 오래된 생성기는 더 이상 사용되지 않습니다. 사용하지 마십시오. 곧 제거 될 것입니다 ({{bug(1083482)}}).</p>
<h3 id="오래된_Generator_메서드들">오래된 Generator 메서드들</h3>
<dl>
<dt><code>Generator.prototype.next() </code>{{non-standard_inline}}</dt>
<dd>{{jsxref("Operators/yield", "yield")}} 표현을 통해 yield된 값을 반환합니다. ES2015 Generator 객체의 <code>next()</code> 에 해당합니다.</dd>
<dt><code>Generator.prototype.close()</code> {{non-standard_inline}}</dt>
<dd>Generator를 닫고, <code>next()가 호출되면 </code>{{jsxref("StopIteration")}} 에러를 던집니다. ES2015 Generator 객체의 <code>return()</code> 에 해당합니다.</dd>
<dt><code>Generator.prototype.send()</code> {{non-standard_inline}}</dt>
<dd>Generator에 값을 전달하기 위해 사용 합니다. 이 값은 {{jsxref("Operators/yield", "yield")}} 표현식에서 반환되고, 다음 {{jsxref("Operators/yield", "yield")}} 표현식으로 부터 yield된 값을 반환합니다. <code>send(x)</code>는 ES2015 Generator 객체의 <code>next(x)에 해당합니다</code>.</dd>
<dt><strong><code>Generator.</code></strong><code>prototype.</code><strong><code>throw()</code> </strong> {{non-standard_inline}}</dt>
<dd>Generator 로 에러를 throw합니다. ES2015 Generator 객체의 <code>throw()</code> 에 해당합니다.</dd>
</dl>
<h3 id="오래된_Generator_예제">오래된 Generator 예제</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>{{CompatibilityTable}}</p>
<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>{{CompatChrome(39.0)}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Feature</th>
<th>Android</th>
<th>Android Webview</th>
<th>Firefox Mobile (Gecko)</th>
<th>IE Mobile</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
<th>Chrome for Android</th>
</tr>
<tr>
<td>Basic support</td>
<td>{{CompatNo}}</td>
<td>{{CompatChrome(39.0)}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatNo}}</td>
<td>{{CompatChrome(39.0)}}</td>
</tr>
</tbody>
</table>
</div>
<p> </p>
<h2 id="같이_보기">같이 보기</h2>
<h3 id="오래된_Generator">오래된 Generator</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_Generator">ES2015 Generator</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>
|