Start · Sprachen · JavaScript · Referenz · AsyncGeneratorFunction.prototype.prototype

AsyncGeneratorFunction.prototype.prototype

Eigenschaft

Verweist auf das gemeinsame Prototyp-Objekt <code>AsyncGenerator.prototype</code>, von dem die <code>prototype</code>-Objekte aller asynchronen Generatorfunktionen erben.

seit JavaScript ES2018 (Baseline: alle modernen Browser, Node.js 1 Kategorie: method-property

Signatur

AsyncGeneratorFunction.prototype.prototype

Beschreibung

AsyncGeneratorFunction.prototype.prototype ist das interne Objekt %AsyncGeneratorPrototype%, das auch unter dem Namen AsyncGenerator.prototype bekannt ist. Dort sind die Methoden next(), return() und throw() definiert, die jedes asynchrone Generator-Objekt bereitstellt. Außerdem liegt dort das Symbol Symbol.toStringTag mit dem Wert "AsyncGenerator".

Die Vererbungskette hat drei Stufen: Jede mit async function* deklarierte Funktion bekommt ihr eigenes prototype-Objekt. Dessen [[Prototype]] ist AsyncGeneratorFunction.prototype.prototype. Ruft man die Generatorfunktion auf, erbt das zurückgegebene Generator-Objekt von fn.prototype und damit indirekt von diesem gemeinsamen Objekt.

Da AsyncGeneratorFunction kein globaler Bezeichner ist, kommt man über (async function* () {}).constructor an das Objekt heran. Das braucht man vor allem für Introspektion, zum Prüfen, ob ein Objekt ein asynchroner Generator ist, oder für Polyfills und Tests. Im normalen Anwendungscode greift man selten direkt darauf zu.

Rückgabewert

Typ
AsyncGenerator (Object)
Beschreibung
Das gemeinsame Prototyp-Objekt AsyncGenerator.prototype mit den Methoden next(), return() und throw(). Eigenschafts-Attribute: writable: false, enumerable: false, configurable: true.

Beispiele

An den gemeinsamen Prototyp herankommen

// AsyncGeneratorFunction ist nicht global – über den Konstruktor holen
const AsyncGeneratorFunction = (async function* () {}).constructor;
const AsyncGeneratorPrototype = AsyncGeneratorFunction.prototype.prototype;

console.log(Object.prototype.toString.call(AsyncGeneratorPrototype));
console.log(Object.getOwnPropertyNames(AsyncGeneratorPrototype));
[object AsyncGenerator] [ 'constructor', 'next', 'return', 'throw' ]

Prototypkette eines asynchronen Generators nachvollziehen

const AsyncGeneratorPrototype = (async function* () {}).constructor.prototype.prototype;

async function* ladeSeiten() {
  yield 'Seite 1';
  yield 'Seite 2';
}

const it = ladeSeiten();

console.log(Object.getPrototypeOf(it) === ladeSeiten.prototype);
console.log(Object.getPrototypeOf(ladeSeiten.prototype) === AsyncGeneratorPrototype);
console.log(it.next === AsyncGeneratorPrototype.next);
true true true

Prüfen, ob ein Objekt ein asynchroner Generator ist

const AsyncGeneratorPrototype = (async function* () {}).constructor.prototype.prototype;

const isAsyncGenerator = (obj) =>
  obj !== null && typeof obj === 'object' && AsyncGeneratorPrototype.isPrototypeOf(obj);

async function* stream() { yield 1; }
function* syncGen() { yield 1; }

console.log(isAsyncGenerator(stream()));
console.log(isAsyncGenerator(syncGen()));
console.log(isAsyncGenerator({ next: async () => ({}) }));
true false false

Eigenschafts-Attribute auslesen

const AsyncGeneratorFunction = (async function* () {}).constructor;
const { writable, enumerable, configurable } =
  Object.getOwnPropertyDescriptor(AsyncGeneratorFunction.prototype, 'prototype');

console.log({ writable, enumerable, configurable });
{ writable: false, enumerable: false, configurable: true }

// Wichtig · Fallstricke

  • Nicht verwechseln: AsyncGeneratorFunction.prototype ist der Prototyp der Generatorfunktionen. AsyncGeneratorFunction.prototype.prototype ist der Prototyp, von dem die prototype-Objekte dieser Funktionen erben, also die Grundlage für die Generator-Objekte.
  • Die Eigenschaft ist writable: false. Eine Zuweisung wird im nicht-strikten Modus stillschweigend ignoriert und löst im strikten Modus einen TypeError aus.
  • Methoden, die man hier ergänzt (Monkey-Patching), stehen in allen asynchronen Generatoren der Laufzeitumgebung zur Verfügung. Das kann mit künftigen Standardmethoden kollidieren und sollte nur bei Polyfills passieren.
  • Die Prüfung mit isPrototypeOf funktioniert nur innerhalb desselben Realms. Generatoren aus einem iframe oder einem anderen vm-Kontext haben einen anderen Prototyp.
  • Ein selbst gebauter asynchroner Iterator (ein Objekt mit next() und Symbol.asyncIterator) erbt nicht von diesem Prototyp. Für for await...of reicht das Iterator-Protokoll.
  • Wird seit ES2018 unterstützt: Chrome 63+, Firefox 55+, Safari 12+, Node.js 10+.