---
hide_title: false
hide_table_of_contents: false
pagination_next: null
pagination_prev: null
---
# Object.setPrototypeOf()

The **`Object.setPrototypeOf()`** method sets the prototype (i.e., the internal `[[Prototype]]` property) of a specified object to another object or [`null`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/null).

> **Warning:** Changing the `[[Prototype]]` of an object is, by the nature of how modern JavaScript engines optimize property accesses, currently a very slow operation in every browser and JavaScript engine. In addition, the effects of altering inheritance are subtle and far-flung, and are not limited to the time spent in the `Object.setPrototypeOf(...)` statement, but may extend to **_any_** code that has access to any object whose `[[Prototype]]` has been altered. You can read more in [JavaScript engine fundamentals: optimizing prototypes](https://mathiasbynens.be/notes/prototypes).
>
> Because this feature is a part of the language, it is still the burden on engine developers to implement that feature performantly (ideally). Until engine developers address this issue, if you are concerned about performance, you should avoid setting the `[[Prototype]]` of an object. Instead, create a new object with the desired `[[Prototype]]` using [`Object.create()`](../../globals/Object/create.mdx).

## Syntax

```js
Object.setPrototypeOf(obj, prototype)
```

### Parameters

- `obj`
  - : The object which is to have its prototype set.
- `prototype`
  - : The object's new prototype (an object or [`null`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/null)).

### Return value

The specified object.

### Exceptions

- [`TypeError`](../../globals/TypeError/TypeError.mdx)
  - : Thrown if one of the following conditions is met:
    - The `obj` parameter is [non-extensible](../../globals/Object/isExtensible.mdx), or it's an [immutable prototype exotic object](https://tc39.es/ecma262/#sec-immutable-prototype-exotic-objects), such as `Object.prototype`.
    - The `prototype` parameter is not an object or [`null`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/null).

## Description

`Object.setPrototypeOf()` is generally considered the proper way to set the prototype of an object.

If the `obj` parameter is not an object (e.g. number, string, etc.), this method does nothing.

For security concerns, there are certain built-in objects that are designed to have an _immutable prototype_. This prevents prototype pollution attacks, especially [proxy-related ones](https://github.com/tc39/ecma262/issues/272). The core language only specifies `Object.prototype` as an immutable prototype exotic object, whose prototype is always `null`.

```js
Object.isExtensible(Object.prototype); // true; you can add more properties
Object.setPrototypeOf(Object.prototype, {}); // TypeError: Immutable prototype object '#<Object>' cannot have their prototype set
```
