大网数据广告活动
Web 标准与前端开发

TypeError: can't set prototype of this object

· 52CE 我爱测

The JavaScript exception "can't set prototype of this object" occurs when attempting to set the prototype of an object, but the object's prototype is frozen, either by being a built-in immutable prototype object, or by being non-extensible.

The JavaScript exception "can't set prototype of this object" occurs when attempting to set the prototype of an object, but the object's prototype is frozen, either by being a built-in immutable prototype object, or by being non-extensible.

Message

TypeError: Immutable prototype object 'Object.prototype' cannot have their prototype set (V8-based)
TypeError: #<Object> is not extensible (V8-based)
TypeError: can't set prototype of this object (Firefox)
TypeError: Cannot set prototype of immutable prototype object (Safari)
TypeError: Attempted to assign to readonly property. (Safari)

Error type

TypeError

What went wrong?

You are using one of the prototype-mutating methods—most notably, Object.setPrototypeOf()—on an object whose prototype is immutable. Some built-in objects have immutable prototypes, such as Object.prototype and window, for security reasons. User objects can also prevent prototype changes by using Object.preventExtensions(), Object.seal(), or Object.freeze().

Examples

Changing the prototype of a built-in object

A selected few built-in objects have immutable prototypes. For example, you cannot change the prototype of Object.prototype:

Object.setPrototypeOf(Object.prototype, {});

This prevents you from being able to arbitrarily change the behavior of all objects in the system. The prototype of Object.prototype is always null. However, other built-in prototype objects, such as Function.prototype and Array.prototype, are not protected by default in this regard.

Changing the prototype of a non-extensible object

If you make an object non-extensible, you cannot change its prototype either:

const obj = {};
Object.preventExtensions(obj);
Object.setPrototypeOf(obj, {});
// TypeError: can't set prototype of this object

See also

  • Object.setPrototypeOf()
  • Object.preventExtensions()

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。