Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

GeneratorFunction

BaselineWidely available

TheGeneratorFunction object provides methods forgenerator functions. In JavaScript, every generator function is actually aGeneratorFunction object.

Note thatGeneratorFunction isnot a global object. It can be obtained with the following code:

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

GeneratorFunction is a subclass ofFunction.

Try it

const GeneratorFunction = function* () {}.constructor;const foo = new GeneratorFunction(`  yield 'a';  yield 'b';  yield 'c';`);let str = "";for (const val of foo()) {  str += val;}console.log(str);// Expected output: "abc"

Constructor

GeneratorFunction()

Creates a newGeneratorFunction object.

Instance properties

Also inherits instance properties from its parentFunction.

These properties are defined onGeneratorFunction.prototype and shared by allGeneratorFunction instances.

GeneratorFunction.prototype.constructor

The constructor function that created the instance object. ForGeneratorFunction instances, the initial value is theGeneratorFunction constructor.

GeneratorFunction.prototype.prototype

All generator functions share the sameprototype property, which isGenerator.prototype. Each generator function created with thefunction* syntax or theGeneratorFunction() constructor also has its ownprototype property, whose prototype isGeneratorFunction.prototype.prototype. When the generator function is called, itsprototype property becomes the prototype of the returned generator object.

GeneratorFunction.prototype[Symbol.toStringTag]

The initial value of the[Symbol.toStringTag] property is the string"GeneratorFunction". This property is used inObject.prototype.toString().

These properties are own properties of eachGeneratorFunction instance.

prototype

Used when the function is used as a constructor with thenew operator. It will become the new object's prototype.

Instance methods

Inherits instance methods from its parentFunction.

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-generatorfunction-objects

Browser compatibility

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp