GeneratorFunction
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since 2016年9月.
GeneratorFunction
オブジェクトは、ジェネレーター関数のメソッドを提供します。JavaScript では、すべてのジェネレーター関数は実際には GeneratorFunction
オブジェクトです。
GeneratorFunction
はグローバルオブジェクトではないことに注意してください。次のコードを評価することによって得ることができます。
const GeneratorFunction = function* () {}.constructor;
GeneratorFunction
は Function
のサブクラスです。
試してみましょう
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);
// 予想される結果: "abc"
コンストラクター
GeneratorFunction()
-
新しい
GeneratorFunction
オブジェクトを生成します。
インスタンスプロパティ
親である Function
から継承したプロパティもあります。
これらのプロパティは GeneratorFunction.prototype
で定義されており、すべての GeneratorFunction
インスタンスで共有されます。
GeneratorFunction.prototype.constructor
-
インスタンスオブジェクトを作成するコンストラクター関数です。
GeneratorFunction
インスタンスの場合、初期値はGeneratorFunction
コンストラクターです。 GeneratorFunction.prototype.prototype
-
すべてジェネレーター関数は、同じ
prototype
プロパティを共有しており、これはGenerator.prototype
です。function*
構文またはGeneratorFunction()
コンストラクターで生成されたそれぞれのジェネレーター関数も、自身のprototype
プロパティを保有しています。このプロパティのプロトタイプはGeneratorFunction.prototype.prototype
です。ジェネレーター関数が呼び出されると、そのprototype
プロパティが返されるジェネレータオブジェクトのプロトタイプとなります。 GeneratorFunction.prototype[Symbol.toStringTag]
-
[Symbol.toStringTag]
プロパティの初期値は文字列"GeneratorFunction"
です。このプロパティはObject.prototype.toString()
で使用されています。
これらのプロパティは、それぞれのGeneratorFunction
インスタンスが自分自身で持っているプロパティです。
インスタンスメソッド
親である Function
から継承したメソッドがあります。
仕様書
Specification |
---|
ECMAScript® 2026 Language Specification> # sec-generatorfunction-objects> |
ブラウザーの互換性
Loading…