Published Apr 15 2019
Set the prototype of an object.
While youβre here, see my guide on JavaScript Prototypal Inheritance
Accepts two arguments: the object and the prototype.
Usage:
Object.setPrototypeOf(object, prototype)
Example:
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
I wrote an entire book on this topic π
I also got a super cool course π
© 2023 Flavio Copes
using
Notion to Site.
Follow on Twitter
Solopreneur? Wannabe? Adventure awaits