Этот перевод не завершён. Пожалуйста, помогите перевести эту статью с английского

Генератор - это объект, возвращаемый функцией-генератором и соответствующий как "Итерируемому" протоколу, так и протоколу "Итератор".


function* gen() { 
  yield 1;
  yield 2;
  yield 3;

var g = gen(); // "Generator { }"

Возвращает значение, полученное выражением yield.
Возвращает заданное значение и заканчивает генератор.
Выдает ошибку генератора.


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

function* idMaker() {
    var index = 0;
        yield index++;

var gen = idMaker(); // "Generator { }"

console.log(; // 0
console.log(; // 1
console.log(; // 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 (баг 1083482).

Legacy generator methods
Returns a value yielded by the yield expression. This corresponds to next() in the ES2015 generator object.
Closes the generator, so that when calling next() an StopIteration error will be thrown. This corresponds to the return() method in the ES2015 generator object.
Used to send a value to a generator. The value is returned from the yield expression, and returns a value yielded by the next yield expression. send(x) corresponds to next(x) in the ES2015 generator object.
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(;   // 1
console.log(it.send(10)); // 20
console.log(it.close());  // undefined
console.log(;   // throws StopIteration (as the generator is now closed)


Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
Определение 'Generator objects' в этой спецификации.
Стандарт Initial definition.
ECMAScript Latest Draft (ECMA-262)
Определение 'Generator objects' в этой спецификации.

Browser compatibility

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 39.0 (Да) Нет Нет Нет
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support Нет 39.0 (Да) Нет Нет Нет 39.0

See also

Legacy generators

ES2015 generators