The setPrototypeOf method in JavaScript
The Object.setPrototypeOf()
method in JavaScript, it allows you to dynamically set the prototype [[Prototype]]
for the specified object. This is a way to change the prototype of an object after it is created.
Object.setPrototypeOf(obj, prototype);
Here, obj
is an object whose prototype needs to be installed or modified.
And `prototype’ is a new prototype.
Let’s take a small example.
//Prototype
of let skillsOfGirls = {
intro() {
console.log(`Hi, my name is ${this.name }`)
},
dance() {
console.log(`${this.name } dancing!`)
}
};
//New object
let girl = {
name: "Elle"
};
//Specify a prototype for the girl
Object.setPrototypeOf(girl, skillsOfGirls);
//We turn to the methods and use
girl.intro(); // Hi, my name is Elle
girl.dance(); // Elle dances"
Here, the girl
object gets access to the intro
and dance
methods through the ‘skillsOfGirls` prototype
Changing the prototype on the fly can lead to performance issues.
This method is useful for understanding how prototypes and inheritance work in JavaScript.
In most cases, it is better to use Object.create()
or classes (ES6) to work with prototypes.