class Animal {
constructor({ name }) {
this.name = name
}
}
class Dog extends Animal {
constructor(props){
super(props)
this.type = 'DOG'
}
}
class Cat extends Animal {
constructor(props){
super(props)
this.type = 'CAT'
}
}
// Animal.js
function Animal (props) {
this.name = props.name
}
// Dog.js
function Dog (props){
Animal.call(this, props)
this.type = 'DOG'
}
Dog.prototype = Object.create(Animal.prototype)
Dog.prototype.constructor = Dog
// Cat.js
function Cat (props){
Animal.call(this, props)
this.type = 'CAT'
}
Cat.prototype = Object.create(Animal.prototype)
Cat.prototype.constructor = Cat
var dog = new Dog({ name: 'cachorrinho' })
var cat = new Cat({ name: 'gatineo' })
console.log('is cachorrinho an animal?', dog instanceof Animal)
console.log('is cachorrinho a dog?', dog instanceof Dog)
console.log('is cachorrinho a cat?', dog instanceof Cat)
console.log('is gatineo an animal?', cat instanceof Animal)