GeneratorFunction() constructor
The GeneratorFunction() constructor creates {{jsxref("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 {{jsxref("Function/Function", "Function()")}} description apply to GeneratorFunction().
Syntax
new GeneratorFunction(functionBody)
new GeneratorFunction(arg1, functionBody)
new GeneratorFunction(arg1, arg2, functionBody)
new GeneratorFunction(arg1, arg2, /* …, */ argN, functionBody)
GeneratorFunction(functionBody)
GeneratorFunction(arg1, functionBody)
GeneratorFunction(arg1, arg2, functionBody)
GeneratorFunction(arg1, arg2, /* …, */ argN, functionBody)
[!NOTE]
GeneratorFunction()can be called with or withoutnew. Both create a newGeneratorFunctioninstance.
Parameters
See {{jsxref("Function/Function", "Function()")}} .
Examples
Creating and using a GeneratorFunction() constructor
const GeneratorFunction = function* () {}.constructor;
const g = new GeneratorFunction("a", "yield a * 2");
const iterator = g(10);
console.log(iterator.next().value); // 20
Specifications
{{Specifications}}
Browser compatibility
{{Compat}}
See also
function*function*expressionFunction()constructor- Iterators and generators guide
{{jsxref("Functions", "Functions", "", 1)}}