Last Updated: February 25, 2016
·
731
· 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);

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside