To tłumaczenie jest niekompletne. Pomóż przetłumaczyć ten artykuł z języka angielskiego.

Deklaracja function*  (Słowo kluczowe function przed gwiazdką) definiuje funkcję generatora, która zwraca Generator obiekt.

Możesz także zdefinować funkcje generatora używając GeneratorFunction konstruktora oraz function* expression.


function* name([param[, param[, ... param]]]) {
The function name.
The name of an argument to be passed to the function. A function can have up to 255 arguments.
The statements comprising the body of the function.


Generators are functions which can be exited and later re-entered. Their context (variable bindings) will be saved across re-entrances.

Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's next() method is called, the generator function's body is executed until the first yield expression, which specifies the value to be returned from the iterator or, with yield*, delegates to another generator function. The next() method returns an object with a value property containing the yielded value and a done property which indicates whether the generator has yielded its last value as a boolean. Calling the next() method with an argument will resume the generator function execution, replacing the yield statement where execution was paused with the argument from next()


Simple example

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

var gen = idMaker();

console.log(; // 0
console.log(; // 1
console.log(; // 2
console.log(; // undefined
// ...

Example with yield*

function* anotherGenerator(i) {
  yield i + 1;
  yield i + 2;
  yield i + 3;

function* generator(i) {
  yield i;
  yield* anotherGenerator(i);
  yield i + 10;

var gen = generator(10);

console.log(; // 10
console.log(; // 11
console.log(; // 12
console.log(; // 13
console.log(; // 20

Passing arguments into Generators

function* logGenerator() {

var gen = logGenerator();

// the first call of next executes from the start of the function
// until the first yield statement;'pretzel'); // pretzel'california'); // california'mayonnaise'); // mayonnaise

Generators are not constructable

function* f() {}
var obj = new f; // throws "TypeError: f is not a constructor"


Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'function*' in that specification.
Standard Initial definition.
ECMAScript 2016 (ECMA-262)
The definition of 'function*' in that specification.
Standard Changed that generators should not have [[Construct]] trap and will throw when used with new.
ECMAScript 2017 Draft (ECMA-262)
The definition of 'function*' in that specification.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer  Edge Opera Safari (WebKit)
Basic support 39.0 26.0 (26.0) No support 13 26 10
yield* (Yes) 27.0 (27.0) No support 13 26 10
IteratorResult object instead of throwing (Yes) 29.0 (29.0) No support 13 (Yes) ?
Not constructable with new as per ES2016 (Yes) 43.0 (43.0) ? ? ? ?
Trailing comma in parameters ? 52.0 (52.0) ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support No support (Yes) 26.0 (26.0) No support No support 10 39.0
yield* No support (Yes) 27.0 (27.0) No support No support 10 (Yes)
IteratorResult object instead of throwing No support ? 29.0 (29.0) No support No support ? (Yes)
Not constructable with new as per ES2016 ? ? 43.0 (43.0) ? ? ? ?
Trailing comma in parameters ? ? 52.0 (52.0) ? ? ? ?

Firefox-specific notes

Generators and iterators in Firefox versions before 26

Older Firefox versions implement an older version of the generators proposal. In the older version, generators were defined using a regular function keyword (without an asterisk) among other differences. See Legacy generator function for further information.

IteratorResult object returned instead of throwing

Starting with Gecko 29 (Firefox 29 / Thunderbird 29 / SeaMonkey 2.26), the completed generator function no longer throws a TypeError "generator has already finished". Instead, it returns an IteratorResult object like { value: undefined, done: true } (błąd 958951).

See also

Autorzy i etykiety dokumentu

 Autorzy tej strony: labs4apps
 Ostatnia aktualizacja: labs4apps,