Skip to content

Instantly share code, notes, and snippets.

@lucasdaiki
Last active August 3, 2017 20:23
Show Gist options
  • Save lucasdaiki/06d24b3ec92e9c6c811edcb5793d7412 to your computer and use it in GitHub Desktop.
Save lucasdaiki/06d24b3ec92e9c6c811edcb5793d7412 to your computer and use it in GitHub Desktop.
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

Testing

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment