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…