Object

Object.setPrototypeOf

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

Syntax

JavaScript
Object.setPrototypeOf(obj, prototype)

Parameters

ParameterTypeDescription
objobjectThe object whose prototype is to be set
prototypeobject | nullThe new prototype

Return Value

The specified object

Examples

Basic Usage
const obj = { a: 1 };
const proto = { greet() { return 'hello'; } };
Object.setPrototypeOf(obj, proto);
console.log((obj as any).greet()); // 'hello'
Practical Example
const animal = { speak() { return 'generic sound'; } };
const dog = { speak() { return 'woof'; } };
Object.setPrototypeOf(dog, animal);
console.log(dog.speak()); // 'woof'
Advanced Usage
// Prefer Object.create() over setPrototypeOf for performance
const base = { type: 'base' };
const child = Object.create(base);

Understanding Object.setPrototypeOf

The Object.setPrototypeOf method in JavaScript sets the prototype of a specified object to another object or null. It belongs to the Object object and is one of the most widely used methods for working with object values in modern JavaScript and TypeScript applications.

The method signature is Object.setPrototypeOf(obj, prototype). It accepts 2 parameters: obj, prototype. When called, it returns the specified object. Understanding when and how to use setPrototypeOf() helps you write more expressive, readable code.

Common use cases for Object.setPrototypeOf include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like object-getprototypeof, object-create, enabling you to chain operations together for complex data manipulation pipelines.

Browser support for Object.setPrototypeOf is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.

Browser Compatibility

Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.

Related Methods

More Object Methods

Other methods in the Object object

Related Tools

More Object Methods

Explore JavaScript Methods

Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.