类型错误:无法设置原型:会造成原型链循环

JavaScript 异常“TypeError:无法设置原型:当一个对象的原型被设置为一个对象,使得 原型链 变成循环(ab 在它们的原型链中都有彼此)时,就会导致“原型链循环”。

¥The JavaScript exception "TypeError: can't set prototype: it would cause a prototype chain cycle" occurs when an object's prototype is set to an object such that the prototype chain becomes circular (a and b both have each other in their prototype chains).

信息

¥Message

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

错误类型

¥Error type

TypeError

什么地方出了错?

¥What went wrong?

原型链中引入了循环,也称为循环。这意味着,当走这条原型链时,同一个地方会被一遍又一遍地访问,而不是最终到达 null

¥A loop, also called a cycle, was introduced in a prototype chain. That means that when walking this prototype chain, the same place would be accessed over and over again, instead of eventually reaching null.

这个错误是在设置原型时抛出的。在像 Object.setPrototypeOf(a, b) 这样的操作中,如果 a 已经存在于 b 的原型链中,就会抛出这个错误。

¥This error is thrown at the time of setting the prototype. In an operation like Object.setPrototypeOf(a, b), if a already exists in the prototype chain of b, this error will be thrown.

示例

¥Examples

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

也可以看看