Object.setPrototypeOf()
setPrototypeOf(obj, prototype) Returns:
object · Added in vES6 · Updated March 16, 2026 · Object Methods object prototype prototype-of
The Object.setPrototypeOf() method sets the prototype of an object to another object or null. This method changes the object’s internal [[Prototype]] property, modifying its prototype chain at runtime. While powerful, frequent prototype changes can impact performance.
Syntax
Object.setPrototypeOf(obj, prototype)
Parameters
| Parameter | Type | Description |
|---|---|---|
obj | object | The object whose prototype to set |
prototype | object | null | The new prototype for the object |
Examples
Setting a prototype on a new object
const animal = {
speak() {
return `${this.name} makes a sound`;
}
};
const dog = { name: 'Rex' };
Object.setPrototypeOf(dog, animal);
console.log(dog.speak()); // "Rex makes a sound"
Changing prototype of existing object
const proto1 = { a: 1 };
const obj = { b: 2 };
Object.setPrototypeOf(obj, proto1);
console.log(obj.a); // 1
console.log(obj.b); // 2
Setting prototype to null
const obj = { x: 1 };
Object.setPrototypeOf(obj, null);
console.log(Object.getPrototypeOf(obj)); // null
console.log(obj.toString); // undefined - no inherited methods
Mixin-like behavior
const canFly = {
fly() {
return `${this.name} is flying`;
}
};
const bird = { name: 'Sparrow' };
Object.setPrototypeOf(bird, canFly);
console.log(bird.fly()); // "Sparrow is flying"
Common Patterns
Dynamic object composition
function extendObject(target, mixins) {
for (const mixin of mixins) {
Object.setPrototypeOf(target, mixin);
}
return target;
}
const canSwim = { swim() { return 'swimming'; } };
const canWalk = { walk() { return 'walking'; } };
const amphibian = extendObject({ name: 'Frog' }, [canSwim, canWalk]);
console.log(amphibian.swim()); // "swimming"
console.log(amphibian.walk()); // "walking"
Performance Note
Changing prototypes is expensive because it affects property lookups. For performance-critical code, use Object.create() to set the prototype at creation time instead.
See Also
- Object.create() — Create object with specified prototype
- Object.keys() — Get array of object keys