GeneratorFunction() 构造函数

GeneratorFunction() 构造函数创建 GeneratorFunction 对象。

¥The GeneratorFunction() constructor creates GeneratorFunction objects.

请注意,GeneratorFunction 不是全局对象。可以通过以下代码获取:

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

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

GeneratorFunction() 构造函数不适合直接使用,Function() 描述中提到的所有注意事项均适用于 GeneratorFunction()

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

语法

¥Syntax

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

注意:可以使用或不使用 new 来调用 GeneratorFunction()。两者都会创建一个新的 GeneratorFunction 实例。

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

参数

¥Parameters

参见 Function()

¥See Function().

示例

¥Examples

创建和使用 GeneratorFunction() 构造函数

¥Creating and using a GeneratorFunction() constructor

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

规范

Specification
ECMAScript Language Specification
# sec-generatorfunction-constructor

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看