Last Updated: February 25, 2016
·
494
· hlmerscher

Javascript inheritance

function Animal(name) {
  this.name = name;

  this.breathe = function() {
    // do something
  }
}

function Dog(name) {
  Animal.call(this, name);

  this.bark = function() {
    // do something
  }
}

Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;

var dog = new Dog("Rex");

console.log(dog instanceof Dog);
console.log(dog instanceof Animal);
console.log(Dog.prototype.constructor);
console.log(Dog.name);