Last active
August 29, 2015 14:02
-
-
Save changemewtf/6d25aa29c3ab7b7545aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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