GeneratorFunction() constructor

The GeneratorFunction() constructor creates GeneratorFunction objects.

Note that GeneratorFunction is not a global object. It can be obtained with the following code:

const GeneratorFunction = function* () {}.constructor;

The GeneratorFunction() constructor is not intended to be used directly, and all caveats mentioned in the Function() description apply to GeneratorFunction().


new GeneratorFunction(functionBody)
new GeneratorFunction(arg0, functionBody)
new GeneratorFunction(arg0, arg1, functionBody)
new GeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody)

GeneratorFunction(arg0, functionBody)
GeneratorFunction(arg0, arg1, functionBody)
GeneratorFunction(arg0, arg1, /* … ,*/ argN, functionBody)

Note: GeneratorFunction() can be called with or without new. Both create a new GeneratorFunction instance.



Creating and using a GeneratorFunction() constructor

const GeneratorFunction = function* () {}.constructor;
const g = new GeneratorFunction("a", "yield a * 2");
const iterator = g(10);
console.log(; // 20


ECMAScript Language Specification
# sec-generatorfunction-constructor

Browser compatibility

BCD tables only load in the browser

See also