diff options
author | Peter Bengtsson <mail@peterbe.com> | 2020-12-10 08:37:18 -0500 |
---|---|---|
committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-10 08:37:18 -0500 |
commit | 65cc6eabd71b1bceccf6fd3d3d4970c2955f3784 (patch) | |
tree | 3d54e994e6b6ff762841fe4c3eb60e87130589a5 /files/ru/web/javascript/reference/global_objects/generator | |
parent | a065e04d529da1d847b5062a12c46d916408bf32 (diff) | |
download | translated-content-65cc6eabd71b1bceccf6fd3d3d4970c2955f3784.tar.gz translated-content-65cc6eabd71b1bceccf6fd3d3d4970c2955f3784.tar.bz2 translated-content-65cc6eabd71b1bceccf6fd3d3d4970c2955f3784.zip |
dump 2020-12-10
Diffstat (limited to 'files/ru/web/javascript/reference/global_objects/generator')
-rw-r--r-- | files/ru/web/javascript/reference/global_objects/generator/index.html | 107 |
1 files changed, 15 insertions, 92 deletions
diff --git a/files/ru/web/javascript/reference/global_objects/generator/index.html b/files/ru/web/javascript/reference/global_objects/generator/index.html index b5fe95a5bf..385792c2e0 100644 --- a/files/ru/web/javascript/reference/global_objects/generator/index.html +++ b/files/ru/web/javascript/reference/global_objects/generator/index.html @@ -16,17 +16,23 @@ translation_of: Web/JavaScript/Reference/Global_Objects/Generator <p><code><strong>Генератор</strong></code> - это объект, возвращаемый {{jsxref("Statements/function*", "функцией-генератором", "", 1)}} и соответствующий как <a href="/ru/docs/Web/JavaScript/Reference/Iteration_protocols#iterable">"Итерируемому" протоколу</a>, так и <a href="/ru/docs/Web/JavaScript/Reference/Iteration_protocols#iterator">протоколу "Итератор"</a>.</p> -<h2 id="Синтаксис">Синтаксис</h2> +<h2 id="Конструктор">Конструктор</h2> -<pre class="syntaxbox">function* gen() { +<p>Этот объект не может быть инстанциирован напрямую. Вместо этого, экемпляр <code>Generator</code> может быть возвращён из <a href="/ru/docs/Web/JavaScript/Reference/Global_Objects/GeneratorFunction">функции-генератора</a>:</p> + +<pre class="notranslate line-numbers language-html">function* generator() { yield 1; yield 2; yield 3; } -var g = gen(); // "Generator { }"</pre> +const gen = generator(); // "Generator { }" + +console.log(gen.next().value); // 1 +console.log(generator().next().value); // 1 +console.log(generator().next().value); // 1</pre> -<h2 id="Методы">Методы</h2> +<h2 id="Методы_экземпляра">Методы экземпляра</h2> <dl> <dt>{{jsxref("Generator.prototype.next()")}}</dt> @@ -41,7 +47,7 @@ var g = gen(); // "Generator { }"</pre> <h3 id="Бесконечный_Итератор">Бесконечный Итератор</h3> -<pre class="brush: js">function* idMaker() { +<pre class="brush: js notranslate">function* idMaker() { var index = 0; while(true) yield index++; @@ -54,39 +60,7 @@ console.log(gen.next().value); // 1 console.log(gen.next().value); // 2 // ...</pre> -<h2 id="Legacy_generator_objects">Legacy generator objects</h2> - -<p>Firefox (SpiderMonkey) also implements an earlier version of generators in <a href="/en-US/docs/Web/JavaScript/New_in_JavaScript/1.7">JavaScript 1.7</a>, where the star (*) in the function declaration was not necessary (you just use the <code>yield</code> keyword in the function body). However, legacy generators are deprecated. Do not use them; they are going to be removed ({{bug(1083482)}}).</p> - -<h3 id="Legacy_generator_methods">Legacy generator methods</h3> - -<dl> - <dt><code>Generator.prototype.next() </code>{{non-standard_inline}}</dt> - <dd>Returns a value yielded by the {{jsxref("Operators/yield", "yield")}} expression. This corresponds to <code>next()</code> in the ES2015 generator object.</dd> - <dt><code>Generator.prototype.close()</code> {{non-standard_inline}}</dt> - <dd>Closes the generator, so that when calling <code>next()</code> an {{jsxref("StopIteration")}} error will be thrown. This corresponds to the <code>return()</code> method in the ES2015 generator object.</dd> - <dt><code>Generator.prototype.send()</code> {{non-standard_inline}}</dt> - <dd>Used to send a value to a generator. The value is returned from the {{jsxref("Operators/yield", "yield")}} expression, and returns a value yielded by the next {{jsxref("Operators/yield", "yield")}} expression. <code>send(x)</code> corresponds to <code>next(x)</code> in the ES2015 generator object.</dd> - <dt><strong><code>Generator.</code></strong><code>prototype.</code><strong><code>throw()</code> </strong> {{non-standard_inline}}</dt> - <dd>Throws an error to a generator. This corresponds to the <code>throw()</code> method in the ES2015 generator object.</dd> -</dl> - -<h3 id="Legacy_generator_example">Legacy generator example</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="Specifications">Specifications</h2> +<h2 id="Спецификации">Спецификации</h2> <table class="standard-table"> <tbody> @@ -103,73 +77,22 @@ console.log(it.next()); // throws StopIteration (as the generator is now close <tr> <td>{{SpecName('ESDraft', '#sec-generator-objects', 'Generator objects')}}</td> <td>{{Spec2('ESDraft')}}</td> - <td> </td> + <td></td> </tr> </tbody> </table> -<h2 id="Browser_compatibility">Browser compatibility</h2> +<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>{{Compat("javascript.builtins.Generator")}}</p> <h2 id="See_also">See also</h2> <h3 id="Legacy_generators">Legacy generators</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> |