TypeError: can't set prototype: it would cause a prototype chain cycle

Der JavaScript-Ausnahmefehler "TypeError: can't set prototype: it would cause a prototype chain cycle" tritt auf, wenn das Prototyp eines Objekts so gesetzt wird, dass die Prototypenkette zirkulär wird (a und b enthalten sich gegenseitig in ihren Prototypenketten).

Nachricht

TypeError: Cyclic __proto__ value (V8-based)
TypeError: can't set prototype: it would cause a prototype chain cycle (Firefox)
TypeError: cyclic __proto__ value (Safari)

Fehlertyp

Was ist schiefgelaufen?

Ein Schaltkreis oder Kreislauf wurde in eine Prototypenkette eingeführt. Das bedeutet, dass beim Durchlaufen dieser Prototypenkette ständig derselbe Punkt wiederholt aufgerufen wird, anstatt schließlich null zu erreichen.

Dieser Fehler tritt beim Setzen des Prototyps auf. Bei einer Operation wie Object.setPrototypeOf(a, b), wenn a bereits in der Prototypenkette von b existiert, wird dieser Fehler ausgelöst.

Beispiele

js
const a = {};
Object.setPrototypeOf(a, a);
// TypeError: can't set prototype: it would cause a prototype chain cycle
js
const a = {};
const b = {};
const c = {};
Object.setPrototypeOf(a, b);
Object.setPrototypeOf(b, c);
Object.setPrototypeOf(c, a);
// TypeError: can't set prototype: it would cause a prototype chain cycle

Siehe auch