aboutsummaryrefslogtreecommitdiff
path: root/files/pl/web/javascript/guide/iterators_and_generators
diff options
context:
space:
mode:
authorPeter Bengtsson <mail@peterbe.com>2020-12-08 14:42:52 -0500
committerPeter Bengtsson <mail@peterbe.com>2020-12-08 14:42:52 -0500
commit074785cea106179cb3305637055ab0a009ca74f2 (patch)
treee6ae371cccd642aa2b67f39752a2cdf1fd4eb040 /files/pl/web/javascript/guide/iterators_and_generators
parentda78a9e329e272dedb2400b79a3bdeebff387d47 (diff)
downloadtranslated-content-074785cea106179cb3305637055ab0a009ca74f2.tar.gz
translated-content-074785cea106179cb3305637055ab0a009ca74f2.tar.bz2
translated-content-074785cea106179cb3305637055ab0a009ca74f2.zip
initial commit
Diffstat (limited to 'files/pl/web/javascript/guide/iterators_and_generators')
-rw-r--r--files/pl/web/javascript/guide/iterators_and_generators/index.html165
1 files changed, 165 insertions, 0 deletions
diff --git a/files/pl/web/javascript/guide/iterators_and_generators/index.html b/files/pl/web/javascript/guide/iterators_and_generators/index.html
new file mode 100644
index 0000000000..236d360a92
--- /dev/null
+++ b/files/pl/web/javascript/guide/iterators_and_generators/index.html
@@ -0,0 +1,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 &lt; 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>