GeneratorFunction

GeneratorFunction 对象为 生成器函数 提供方法。在 JavaScript 中,每个生成器函数实际上都是一个 GeneratorFunction 对象。

¥The GeneratorFunction object provides methods for generator functions. In JavaScript, every generator function is actually a GeneratorFunction object.

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

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

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

GeneratorFunctionFunction 的子类。

¥GeneratorFunction is a subclass of Function.

Try it

构造函数

¥Constructor

GeneratorFunction()

创建一个新的 GeneratorFunction 对象。

实例属性

¥Instance properties

还从其父级 Function 继承实例属性。

¥Also inherits instance properties from its parent Function.

这些属性在 GeneratorFunction.prototype 上定义并由所有 GeneratorFunction 实例共享。

¥These properties are defined on GeneratorFunction.prototype and shared by all GeneratorFunction instances.

GeneratorFunction.prototype.constructor

创建实例对象的构造函数。对于 GeneratorFunction 实例,初始值为 GeneratorFunction 构造函数。

GeneratorFunction.prototype.prototype

所有生成器函数共享相同的 prototype 属性,即 Generator.prototype。每个生成器函数实例也有自己的 prototype 属性。当调用生成器函数时,返回的生成器对象继承自生成器函数的 prototype 属性,而 prototype 属性又继承自 GeneratorFunction.prototype.prototype

GeneratorFunction.prototype[@@toStringTag]

@@toStringTag 属性的初始值为字符串 "GeneratorFunction"。该属性在 Object.prototype.toString() 中使用。

实例方法

¥Instance methods

从其父级 Function 继承实例方法。

¥Inherits instance methods from its parent Function.

规范

Specification
ECMAScript Language Specification
# sec-generatorfunction-objects

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看