/

The setPrototypeOf() Method in JavaScript

The setPrototypeOf() Method in JavaScript

Learn all about the setPrototypeOf() method in the JavaScript Object object.

Setting the prototype of an object is made possible with the setPrototypeOf() method.

To dive deeper into JavaScript Prototypal Inheritance, check out my comprehensive guide here.

Syntax:

1
Object.setPrototypeOf(object, prototype)

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
const Animal = {}
Animal.isAnimal = true

const Mammal = Object.create(Animal)
Mammal.isMammal = true

console.log('-------')
Mammal.isAnimal // true

const dog = Object.create(Animal)

dog.isAnimal // true
console.log(dog.isMammal) // undefined

Object.setPrototypeOf(dog, Mammal)

console.log(dog.isAnimal) // true
console.log(dog.isMammal) // true

tags: [“JavaScript”, “setPrototypeOf”, “prototype”, “object”, “inheritance”]