aboutsummaryrefslogtreecommitdiff
path: root/files/pl/web/javascript/reference/operators/yield_star_/index.html
blob: e27a1c3aca3c6e188c824faeae7d53031493075d (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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
---
title: yield*
slug: Web/JavaScript/Reference/Operators/yield*
translation_of: Web/JavaScript/Reference/Operators/yield*
original_slug: Web/JavaScript/Referencje/Operatory/yield*
---
<div>{{jsSidebar("Operators")}}</div>

<p><strong><code>Wyrażenie yield*</code> </strong> służy do wydelegowania działania generatora do innego {{jsxref("Statements/function*", "generatora")}} lub obiektu iterowalnego.</p>

<div>{{EmbedInteractiveExample("pages/js/expressions-yieldasterisk.html")}}</div>

<p class="hidden">Źródło poniższego interaktywnego przykładu przechowywane jest w repozytorium na GitHub. Jeśli chcesz współtworzyć projekt interaktywnych przykładów, sklonuj <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> i wyślij nam pull request.</p>

<h2 id="Składnia">Składnia</h2>

<pre class="syntaxbox"> yield* [[expression]];</pre>

<dl>
 <dt><code>expression</code></dt>
 <dd>Wyrażenie, które zwraca iterowalny obiekt lub generator.</dd>
</dl>

<h2 id="Opis">Opis</h2>

<p><code>yield*</code> iteruje po iterowalnym obiekcie i wywołuje <code>yield</code> z każdą kolejną zwracaną przez niego wartością.</p>

<p>Wartość samego <code>yield*</code> jest wartością zwróconą przez iterator w momencie jego zakończenia (tzn. kiedy <code>done</code> ma wartość <code>true</code>).</p>

<h2 id="Przykłady">Przykłady</h2>

<h3 id="Delegowanie_logiki_do_osobnego_generatora">Delegowanie logiki do osobnego generatora</h3>

<p>W poniższym kodzie wartości <code>yeld</code> dla  <code>g1()</code> zwracane są przy wywołaniu <code>next()</code> dokładnie tak samo jak te, które zwraca <code>yeld</code> generatora <code>g2()</code>.</p>

<pre class="brush: js">function* g1() {
  yield 2;
  yield 3;
  yield 4;
}

function* g2() {
  yield 1;
  yield* g1();
  yield 5;
}

var iterator = g2();

console.log(iterator.next()); // {value: 1, done: false}
console.log(iterator.next()); // {value: 2, done: false}
console.log(iterator.next()); // {value: 3, done: false}
console.log(iterator.next()); // {value: 4, done: false}
console.log(iterator.next()); // {value: 5, done: false}
console.log(iterator.next()); // {value: undefined, done: true}
</pre>

<h3 id="Inne_obiekty_iterowalne">Inne obiekty iterowalne</h3>

<p><code>yield*</code> może wywoływać <code>yield</code> z wartościami dostarczanymi przez inne rodzje obiektów iterowalnych , np. tablice, stringi lub obiekt <code>arguments</code>.</p>

<pre class="brush: js">function* g3() {
  yield* [1, 2];
  yield* '34';
  yield* Array.from(arguments);
}

var iterator = g3(5, 6);

console.log(iterator.next()); // {value: 1, done: false}
console.log(iterator.next()); // {value: 2, done: false}
console.log(iterator.next()); // {value: "3", done: false}
console.log(iterator.next()); // {value: "4", done: false}
console.log(iterator.next()); // {value: 5, done: false}
console.log(iterator.next()); // {value: 6, done: false}
console.log(iterator.next()); // {value: undefined, done: true}
</pre>

<h3 id="Wartość_samego_wyrażenia_yield*">Wartość samego wyrażenia <code>yield*</code></h3>

<p><code>yield*</code> jest wyrażeniem (expression) a nie statement, więc rozwiązuje się do konkretnej wartości.</p>

<pre class="brush: js">function* g4() {
  yield* [1, 2, 3];
  return 'foo';
}

var result;

function* g5() {
  result = yield* g4();
}

var iterator = g5();

console.log(iterator.next()); // {value: 1, done: false}
console.log(iterator.next()); // {value: 2, done: false}
console.log(iterator.next()); // {value: 3, done: false}
console.log(iterator.next()); // {value: undefined, done: true},
                              // g4() zwrócił w tym momencie {value: 'foo', done: true}

console.log(result);          // "foo"
</pre>

<h2 id="Specyfikacje">Specyfikacje</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('ES2015', '#', 'Yield')}}</td>
   <td>{{Spec2('ES2015')}}</td>
   <td>Initial definition.</td>
  </tr>
  <tr>
   <td>{{SpecName('ESDraft', '#', 'Yield')}}</td>
   <td>{{Spec2('ESDraft')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</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 (WebKit)</th>
  </tr>
  <tr>
   <td>Basic support</td>
   <td>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoDesktop("27.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatSafari("10")}}</td>
  </tr>
 </tbody>
</table>
</div>

<div id="compat-mobile">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Feature</th>
   <th>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>{{CompatVersionUnknown}}</td>
   <td>{{CompatGeckoMobile("27.0")}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatUnknown}}</td>
   <td>{{CompatSafari("10")}}</td>
  </tr>
 </tbody>
</table>
</div>

<h2 id="Firefox-specific_notes">Firefox-specific notes</h2>

<ul>
 <li>Starting with Gecko 33 {{geckoRelease(33)}}, the parsing of the yield expression has been updated to conform with the ES2015 specification ({{bug(981599)}}):
  <ul>
   <li>The line terminator restriction is now implemented. No line terminator between "yield" and "*" is allowed. Code like the following will throw a {{jsxref("SyntaxError")}}:
    <pre class="brush: js">function* foo() {
  yield
  *[];
}</pre>
   </li>
  </ul>
 </li>
</ul>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol">The Iterator protocol</a></li>
 <li>{{jsxref("Statements/function*", "function*")}}</li>
 <li>{{jsxref("Operators/function*", "function* expression")}}</li>
 <li>{{jsxref("Operators/yield", "yield")}}</li>
</ul>