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
|
---
title: Iterators and generators
slug: Web/JavaScript/Guide/Iterators_and_Generators
tags:
- JavaScript
- Przewodnik
translation_of: Web/JavaScript/Guide/Iterators_and_Generators
---
<div>{{jsSidebar("JavaScript Guide")}} {{PreviousNext("Web/JavaScript/Guide/Details_of_the_Object_Model", "Web/JavaScript/Guide/Meta_programming")}}</div>
<p class="summary">Przetwarzanie każdego elementu w kolekcji jest bardzo popularną operacją. JavaScript daje wiele możliwości iteracji przez elementy kolekcji: od prostej pętli {{jsxref("Polecenia/for","for")}} do {{jsxref("Obiekty/Array/map","map()")}} i {{jsxref("Obiekty/Array/filter","filter()")}}. Iteratory i Generatory wnoszą pojęcie interacji bezpośredno w natywne funkcjonalności języka i dają możliwość do dostosowania zachowania pętli {{jsxref("Polecenia/for...of","for...of")}}.</p>
<p>Zobacz także:</p>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Iteration_protocols">Iteration protocols</a></li>
<li>{{jsxref("Polecenia/for...of","for...of")}}</li>
<li>{{jsxref("Polecenia/function*","function*")}} i {{jsxref("Obiekty/Generator","Generator")}}</li>
<li>{{jsxref("Operatory/yield","yield")}} i {{jsxref("Operatory/yield*","yield*")}}</li>
</ul>
<h2 id="Iteratory">Iteratory</h2>
<p>Obiekt jest <strong>iteratorem</strong> kiedy wie jak uzyskać dostęp do elementów z kolekcji pojedynczo, podczas gdy śledzi swoją obecną pozycję w tej sekwencji. W języku JavaScript iterator jest obiektem który dostarcza metodę next(), która zwraca następny obiekt w tej sekwencji. Ta metoda zwraca obiekt z dwoma właściwościami: <code>done</code> i <code>value</code>.</p>
<p>Po utworzeniu, obiekt iteratora może być użyty jawnie przez powtarzanie wywołania metody next().</p>
<pre class="brush: js">function makeIterator(array) {
var nextIndex = 0;
return {
next: function() {
return nextIndex < array.length ?
{value: array[nextIndex++], done: false} :
{done: true};
}
};
}</pre>
<p>Po inicjalizacji metoda next() może być wywołana w celu dostępu do par klucz-wartość z obiektu:</p>
<pre class="brush: js">var it = makeIterator(['yo', 'ya']);
console.log(it.next().value); // 'yo'
console.log(it.next().value); // 'ya'
console.log(it.next().done); // true</pre>
<h2 id="Generatory">Generatory</h2>
<p>Pomimo tego, że iteratory są przydatnym narzędziem, ich utworzenie wymaga ostrożnego podejścia, ze względu na potrzebę jawnego utrzymywania ich wewnętrznego stanu. <strong>{{jsxref("Obiekty/Generator","Generators","","true")}}</strong> zapewnia mocną alternatywę: pozwalają one zdefiniować programiście iteratywny algorytm poprzez utworzenie pojedynczej funkcji, która jest wstanie utrzymywać swój wewnętrzny stan.</p>
<p>Generator jest specjalnym typem funkcji, która działa jako fabryka dla iteratorów. Funkcja staje się generatorem, gdy zawiera przynajmniej jedno{{jsxref("Operatory/yield","yield")}} wyrażenie oraz gdy używa {{jsxref("Polecenia/function*","function*")}}.</p>
<pre class="brush: js">function* idMaker() {
var index = 0;
while(true)
yield index++;
}
var gen = idMaker();
console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
// ...</pre>
<h2 id="Iterables">Iterables</h2>
<p>An object is <strong>iterable</strong> if it defines its iteration behavior, such as what values are looped over in a {{jsxref("Polecenia/for...of", "for..of")}} construct. Some built-in types, such as {{jsxref("Array")}} or {{jsxref("Map")}}, have a default iteration behavior, while other types (such as {{jsxref("Object")}}) do not.</p>
<p>In order to be <strong>iterable</strong>, an object must implement the <strong>@@iterator</strong> method, meaning that the object (or one of the objects up its <a href="/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain">prototype chain</a>) must have a property with a {{jsxref("Symbol.iterator")}} key:</p>
<h3 id="User-defined_iterables">User-defined iterables</h3>
<p>We can make our own iterables like this:</p>
<pre class="brush: js">var myIterable = {};
myIterable[Symbol.iterator] = function* () {
yield 1;
yield 2;
yield 3;
};
for (let value of myIterable) {
console.log(value);
}
// 1
// 2
// 3
or
[...myIterable]; // [1, 2, 3]
</pre>
<h3 id="Built-in_iterables">Built-in iterables</h3>
<p>{{jsxref("String")}}, {{jsxref("Array")}}, {{jsxref("TypedArray")}}, {{jsxref("Map")}} and {{jsxref("Set")}} are all built-in iterables, because the prototype objects of them all have a {{jsxref("Symbol.iterator")}} method.</p>
<h3 id="Syntaxes_expecting_iterables">Syntaxes expecting iterables</h3>
<p>Some statements and expressions are expecting iterables, for example the {{jsxref("Polecenia/for...of","for-of")}} loops, {{jsxref("Operatory/Spread_operator","spread operator","","true")}}, {{jsxref("Operatory/yield*","yield*")}}, and {{jsxref("Operatory/Destructuring_assignment","destructuring assignment","","true")}}.</p>
<pre class="brush: js">for (let value of ['a', 'b', 'c']) {
console.log(value);
}
// "a"
// "b"
// "c"
[...'abc']; // ["a", "b", "c"]
function* gen() {
yield* ['a', 'b', 'c'];
}
gen().next(); // { value: "a", done: false }
[a, b, c] = new Set(['a', 'b', 'c']);
a; // "a"
</pre>
<h2 id="Advanced_generators">Advanced generators</h2>
<p>Generators compute their yielded values on demand, which allows them to efficiently represent sequences that are expensive to compute, or even infinite sequences as demonstrated above.</p>
<p>The {{jsxref("Obiekty/Generator/next","next()")}} method also accepts a value which can be used to modify the internal state of the generator. A value passed to <code>next()</code> will be treated as the result of the last <code>yield</code> expression that paused the generator.</p>
<p>Here is the fibonacci generator using <code>next(x)</code> to restart the sequence:</p>
<pre class="brush: js">function* fibonacci() {
var fn1 = 0;
var fn2 = 1;
while (true) {
var current = fn1;
fn1 = fn2;
fn2 = current + fn1;
var reset = yield current;
if (reset) {
fn1 = 0;
fn2 = 1;
}
}
}
var sequence = fibonacci();
console.log(sequence.next().value); // 0
console.log(sequence.next().value); // 1
console.log(sequence.next().value); // 1
console.log(sequence.next().value); // 2
console.log(sequence.next().value); // 3
console.log(sequence.next().value); // 5
console.log(sequence.next().value); // 8
console.log(sequence.next(true).value); // 0
console.log(sequence.next().value); // 1
console.log(sequence.next().value); // 1
console.log(sequence.next().value); // 2</pre>
<p>You can force a generator to throw an exception by calling its {{jsxref("Obiekty/Generator/throw","throw()")}} method and passing the exception value it should throw. This exception will be thrown from the current suspended context of the generator, as if the <code>yield</code> that is currently suspended were instead a <code>throw <em>value</em></code> statement.</p>
<p>If a <code>yield</code> is not encountered during the processing of the thrown exception, then the exception will propagate up through the call to <code>throw()</code>, and subsequent calls to <code>next()</code> will result in the <code>done</code> property being <code>true</code>.</p>
<p>Generators have a {{jsxref("Obiekty/Generator/return","return(value)")}} method that returns the given value and finishes the generator itself.</p>
<p>{{PreviousNext("Web/JavaScript/Guide/Details_of_the_Object_Model", "Web/JavaScript/Guide/Meta_programming")}}</p>
|