blob: 8f87d9925c42ac06353849e1695c0ce6c390bb5c (
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
|
---
title: Generator.prototype.next()
slug: Web/JavaScript/Reference/Global_Objects/Generator/next
translation_of: Web/JavaScript/Reference/Global_Objects/Generator/next
browser-compat: javascript.builtins.Generator.next
---
<div>{{JSRef}}</div>
<p> <code><strong>next</strong></code><strong><code>()</code></strong> 메소드는 <code>done</code>과 <code>value</code> <code>프로퍼티를 가진 객체를 반환한다</code>. 또한 <code><strong>next</strong></code><strong><code>()</code> </strong><code>메소드를 호출할 때 매개변수를 제공하여 그 값을 generator에 전달할 수 있다.</code></p>
<h2 id="문법">문법</h2>
<pre class="syntaxbox"><code><var>gen</var>.next(value)</code></pre>
<h3 id="매개_변수">매개 변수</h3>
<dl>
<dt><code>value</code></dt>
<dd>Generator에 전달할 값</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> (boolean)
<ul>
<li>Iterator(반복자)가 마지막 반복 작업을 마쳤을 경우 <code>true</code>. 만약 iterator(반복자)에 <em>return 값</em>이 있다면 <code>value</code>의 값으로 지정된다.</li>
<li>Iterator(반복자)의 작업이 남아있을 경우 <code>false</code>. Iterator(반복자)에 <code>done</code> 프로퍼티 자체를 특정짓지 않은 것과 동일하다.</li>
</ul>
</li>
<li><code>value</code> - Iterator(반복자)으로부터 반환되는 모든 자바스크립트 값이며 <code>done</code>이 <code>true</code>일 경우 생략될 수 있다.</li>
</ul>
<h2 id="예제">예제</h2>
<h3 id="next()_사용하기"><code>next()</code> 사용하기</h3>
<p>아래의 예시는 간단한 generator와 <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="Generator에_값_전달하기">Generator에 값 전달하기</h3>
<p>이 예시에서는 <code>next</code>가 값과 함께 호출되었다. 첫 번째 호출이 아무것도 출력하지 않은 것은 Generator가 아직 아무런 값도 <code>yield</code> 하지않았기 때문이다. (두 번째 호출과 함께 전달된 정수 2는 Generator 내부의 <code>yield</code> 키워드에 전달되어 <code>value</code>로 할당되었고 <code>console.log</code>로 출력되었다)</p>
<pre class="brush: js">function* gen() {
while(true) {
var value = yield null;
console.log(value);
}
}
var g = gen();
g.next(1);
// "{ value: null, done: false }"
g.next(2);
// "{ value: null, done: false }"
// 2
</pre>
<h2 id="specifications">명세</h2>
<p>{{Specifications}}</p>
<h2 id="browser_compatibility">브라우저 호환성</h2>
<p>{{Compat}}</p>
<h2 id="See_also">See also</h2>
<ul>
<li><code><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function*">function*</a></code></li>
<li><a href="/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators">Iterators and generators</a></li>
</ul>
|