Skip to content

Instantly share code, notes, and snippets.

@changemewtf
Last active August 29, 2015 14:02
Show Gist options
  • Save changemewtf/6d25aa29c3ab7b7545aa to your computer and use it in GitHub Desktop.
Save changemewtf/6d25aa29c3ab7b7545aa to your computer and use it in GitHub Desktop.
function Animal(name) {
this.name = name;
}
Animal.prototype = {
eat: function() { return this.name + " consumes vital sustenance."; }
};
function Dog(name, breed) {
this.name = name;
this.breed = breed;
}
// In stark contrast to classical inheritance, here we are spinning up an "empty"
// Animal object just to use it as the __proto__ of all objects created by Dog
var prototypicalAnimal = new Animal();
Dog.prototype = prototypicalAnimal;
Dog.prototype.bark = function() { return this.name + " barks!"; };
Dog.prototype.procreate = function() { return "A new " + this.breed + " is born."; };
console.log(dog.eat()); //=> Bozo consumes vital sustenance.
console.log(dog.bark()); //=> Bozo barks!
console.log(dog.procreate()); //=> A new terrier is born.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment