Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

TypeError: can't set prototype of this object

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 beingnon-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

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 asObject.prototype andwindow, for security reasons. User objects can also prevent prototype changes by usingObject.preventExtensions(),Object.seal(), orObject.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 ofObject.prototype:

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

This prevents you from being able to arbitrarily change the behavior of all objects in the system. The prototype ofObject.prototype is alwaysnull. However, other built-in prototype objects, such asFunction.prototype andArray.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:

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

See also

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp