3 Different Kinds of Prototypal Inheritance: ES6+ Edition
Eric Elliott
36516
What do you say about the object creation using the following way, is it a good way or does it have any draw backs ?
const Animal = {
create (name) {
return Object.assign(Object.create(this), {
name
})
},
toString () {
return `Hi, my name is ${this.name}`
}
}
const animal1 = Animal.create(‘tiger’)
const animal2 = Animal.create(‘lion’)
const Dog = Object.assign(Object.create(Animal), {
create (name, owner) {
return Object.assign(Object.create(this),
Animal.create(name),
{
owner
})
},
bark () {
return `${this.name} : Woof!`
},
getOwner () {
return `${this.owner} owns ${this.name}`
}
})
const dog1 = Dog.create(‘Rocky’, ‘Kim’)
const dog2 = Dog.create(‘Lucy’,’Joe’)