Object.setPrototypeOf()

Sets the prototype of a specified object to another object or null.

Since ES2015 Spec ↗

Syntax

Object.setPrototypeOf(obj, prototype)

Parameters

NameTypeRequiredDescription
obj object Yes The object whose prototype is to be set.
prototype object | null Yes The new prototype, an object or null.

Returns

object — The same object that was passed in.

Throws

  • TypeError — obj is non-extensible or prototype is not an object/null.

Examples

const proto = { greet() { return 'hi'; } };
const o = {};
Object.setPrototypeOf(o, proto);
console.log(o.greet());
Output
hi
const o = { a: 1 };
Object.setPrototypeOf(o, null);
console.log(Object.getPrototypeOf(o));
Output
null

Notes

Slow - changing an object's prototype deoptimizes property access in all engines. Prefer `Object.create` to set the prototype at creation time.

Browser & runtime support

EnvironmentSince version
chrome 34
firefox 31
safari 9
edge 12
node 0.12

See also