From 65cc6eabd71b1bceccf6fd3d3d4970c2955f3784 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Thu, 10 Dec 2020 08:37:18 -0500 Subject: dump 2020-12-10 --- .../reference/global_objects/generator/index.html | 107 +++------------------ 1 file changed, 15 insertions(+), 92 deletions(-) (limited to 'files/ru/web/javascript/reference/global_objects/generator') 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

Генератор - это объект, возвращаемый {{jsxref("Statements/function*", "функцией-генератором", "", 1)}} и соответствующий как "Итерируемому" протоколу, так и протоколу "Итератор".

-

Синтаксис

+

Конструктор

-
function* gen() {
+

Этот объект не может быть инстанциирован напрямую. Вместо этого, экемпляр Generator может быть возвращён из функции-генератора:

+ +
function* generator() {
   yield 1;
   yield 2;
   yield 3;
 }
 
-var g = gen(); // "Generator { }"
+const gen = generator(); // "Generator { }" + +console.log(gen.next().value); // 1 +console.log(generator().next().value); // 1 +console.log(generator().next().value); // 1
-

Методы

+

Методы экземпляра

{{jsxref("Generator.prototype.next()")}}
@@ -41,7 +47,7 @@ var g = gen(); // "Generator { }"

Бесконечный Итератор

-
function* idMaker() {
+
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
 // ...
-

Legacy generator objects

- -

Firefox (SpiderMonkey) also implements an earlier version of generators in JavaScript 1.7, where the star (*) in the function declaration was not necessary (you just use the yield keyword in the function body). However, legacy generators are deprecated. Do not use them; they are going to be removed ({{bug(1083482)}}).

- -

Legacy generator methods

- -
-
Generator.prototype.next() {{non-standard_inline}}
-
Returns a value yielded by the {{jsxref("Operators/yield", "yield")}} expression. This corresponds to next() in the ES2015 generator object.
-
Generator.prototype.close() {{non-standard_inline}}
-
Closes the generator, so that when calling next() an {{jsxref("StopIteration")}} error will be thrown. This corresponds to the return() method in the ES2015 generator object.
-
Generator.prototype.send() {{non-standard_inline}}
-
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. send(x) corresponds to next(x) in the ES2015 generator object.
-
Generator.prototype.throw() {{non-standard_inline}}
-
Throws an error to a generator. This corresponds to the throw() method in the ES2015 generator object.
-
- -

Legacy generator example

- -
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)
-
- -

Specifications

+

Спецификации

@@ -103,73 +77,22 @@ console.log(it.next()); // throws StopIteration (as the generator is now close - +
{{SpecName('ESDraft', '#sec-generator-objects', 'Generator objects')}} {{Spec2('ESDraft')}} 
-

Browser compatibility

+

Совместимость

-

{{CompatibilityTable}}

-
- - - - - - - - - - - - - - - - - - - -
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(39.0)}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatNo}}
-
-
- - - - - - - - - - - - - - - - - - - - - - - -
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatChrome(39.0)}}{{CompatVersionUnknown}}{{CompatNo}}{{CompatNo}}{{CompatNo}}{{CompatChrome(39.0)}}
-
+

{{Compat("javascript.builtins.Generator")}}

See also

Legacy generators

    -
  • {{jsxref("Statements/Legacy_generator_function", "The legacy generator function", "", 1)}}
  • -
  • {{jsxref("Operators/Legacy_generator_function", "The legacy generator function expression", "", 1)}}
  • -
  • {{jsxref("StopIteration")}}
  • The legacy Iterator protocol
-- cgit v1.2.3-54-g00ecf