Object.setPrototypeOf()
Introduction
Object.setPrototypeOf() is a method in JavaScript that allows you to set the prototype (i.e., the inheritance) of an object to another object. This method is often used in object-oriented programming to create inheritance hierarchies or overriding prototype methods.
WARNING
Object.setPrototypeOf()
is not recommended for use in performance-critical code, as it can have a significant impact on performance. Additionally, modifying the prototype of an object can have unintended consequences and should be done with caution.
How it works ?
The Object.setPrototypeOf()
method takes two parameters: the object you want to set the prototype for, and the object that should become the new prototype. The new prototype can be an object or null
.
When you call Object.setPrototypeOf(obj, prototype)
, JavaScript sets the prototype of obj
to prototype
. If prototype
is null
, the object will not have any prototype and will not inherit any properties or methods.
If you set the prototype of an object to another object, any properties or methods defined on the prototype will become available on the object. If a property or method is not defined on the object itself, JavaScript will look for it on the prototype and any other prototypes in the prototype chain.
Examples
Here are some simple examples of using Object.setPrototypeOf()
:
Example 1: Setting the prototype of an object to another object
const myObj = { a: 1 };
const myProto = { b: 2 };
Object.setPrototypeOf(myObj, myProto);
console.log(myObj.b); // 2
In this example, we create an object myObj
with a property a
set to 1
. We then create another object myProto
with a property b
set to 2
. We use Object.setPrototypeOf()
to set the prototype of myObj
to myProto
. When we log myObj
.b
, we get 2
because b
is defined on the prototype of myObj
.
More example
// Example 1
let person = {
name: "John",
age: 30,
greet() {
console.log(
`Hello, my name is ${this.name} and I am ${this.age} years old.`
);
},
};
let employee = {
position: "Manager",
};
Object.setPrototypeOf(employee, person);
employee.greet(); // outputs "Hello, my name is undefined and I am undefined years old."
// Example 2
let animal = {
makeSound() {
console.log("The animal makes a sound.");
},
};
let dog = {
makeSound() {
console.log("The dog barks.");
},
};
Object.setPrototypeOf(dog, animal);
dog.makeSound(); // outputs "The dog barks."
In Example 1, we create a person
object with a greet
method and an employee
object with a position
property. We then set the prototype of employee
to person
, which means that employee
inherits the greet
method from person. However, when we call employee.greet()
, the this keyword is not defined because the object doesn't have a name
or age
property.
In Example 2, we create an animal
object with a makeSound
method and a dog
object with a makeSound
method that overrides the animal's makeSound
method. We then set the prototype of dog
to animal
, which means that dog
inherits the makeSound
method from animal
.
Detailed explanation
Syntax
Object.setPrototypeOf(obj, prototype);
The obj
parameter is the object whose prototype is to be set. The prototype
parameter is the new prototype object.
Parameter
The obj
and prototype
parameters are both required. The obj
parameter must be an object, and the prototype
parameter must either be null or an object.
Return Value
Object.setPrototypeOf()
returns the modified object. If the object cannot be modified (e.g. if it is a built-in object), a TypeError
is thrown.
Exceptions
If the obj
parameter is not an object, a TypeError
is thrown. If the prototype parameter is not null
or an object, a TypeError
is thrown.
Limitations
The Object.isExtensible()
method only checks if an object is extensible or not. It does not check if a property can be added to an object.
Tips and tricks
-
Object.setPrototypeOf()
should be used with caution because it can slow down the performance of the code. It is also not recommended to use it with built-in objects such as Array or Date because it can cause unexpected behavior. -
Some common use cases for
Object.setPrototypeOf()
include creating inheritance hierarchies and overriding prototype methods. -
When you modify the prototype of an object using
Object.setPrototypeOf()
, you are creating a prototype chain. This means that if a property or method is not found on the object itself, JavaScript will look for it on the object's prototype, and so on up the chain until it reaches theObject.prototype
object.
Ecmascript specification
Object.setPrototypeOf() is part of the ECMAScript 6 (ES6) specification, which was finalized in 2015. It is supported by all modern browsers.
Exercises
- Create a
Person
object with a name property and asayHello
method. Then, create aStudent
object that inherits fromPerson
and has a major property. UseObject.setPrototypeOf()
to set the prototype ofStudent
toPerson
.
Solution
// Define the Person object
let Person = {
name: "John Doe",
sayHello: function () {
console.log(`Hello, my name is ${this.name}`);
},
};
// Define the Student object
let Student = {
major: "Computer Science",
};
// Set the prototype of Student to Person
Object.setPrototypeOf(Student, Person);
// Test the Student object
console.log(Student.name); // 'John Doe'
console.log(Student.major); // 'Computer Science'
Student.sayHello(); // 'Hello, my name is John Doe'
- Create a
Vehicle
object with a start method and a stop method. Then, create aCar
object that inherits fromVehicle
and has a make property. UseObject.setPrototypeOf()
to set the prototype ofCar
toVehicle
.
Solution
// Define the Vehicle object
let Vehicle = {
start: function () {
console.log("Starting the vehicle...");
},
stop: function () {
console.log("Stopping the vehicle...");
},
};
// Define the Car object
let Car = {
make: "Toyota",
};
// Set the prototype of Car to Vehicle
Object.setPrototypeOf(Car, Vehicle);
// Test the Car object
console.log(Car.make); // 'Toyota'
Car.start(); // 'Starting the vehicle...'
Car.stop(); // 'Stopping the vehicle...'
- Create a
Shape
object with acolor
property and agetArea
method. Then, create aSquare
object that inherits fromShape
and has asideLength
property. UseObject.setPrototypeOf()
to set the prototype ofSquare
toShape
. Override thegetArea
method to return the area of the square.
Solution
// Define the Shape object
let Shape = {
color: "red",
getArea: function () {
console.log("This shape has no area.");
},
};
// Define the Square object
let Square = {
sideLength: 5,
getArea: function () {
return this.sideLength * this.sideLength;
},
};
// Set the prototype of Square to Shape
Object.setPrototypeOf(Square, Shape);
// Test the Square object
console.log(Square.color); // 'red'
console.log(Square.sideLength); // 5
console.log(Square.getArea()); // 25
This page was updated on -
Found an error or have feedback on our docs?
Create an issue on GitHub and let us know! Your input helps improve our documentation for everyone in the community.
Report error, send feedback on Github