Skip to content

Instantly share code, notes, and snippets.

@kylefeng
Last active December 31, 2015 00:09
Show Gist options
  • Save kylefeng/7905641 to your computer and use it in GitHub Desktop.
Save kylefeng/7905641 to your computer and use it in GitHub Desktop.
prototype chain
function Animal(name) {
this.name = name;
}
Animal.prototype = {
weight: 0,
eat: function() {
console.log("Animal is eating!");
}
}
function Mammal() {
this.name = "mammal";
}
Mammal.prototype = new Animal("animal");
function Horse(height, weight) {
this.name = "horse";
this.height = height;
this.weight = weight;
}
Horse.prototype = new Mammal();
Horse.prototype.eat = function() {
console.log("Horse is eating grass!");
}
var horse = new Horse(100, 300);
console.log(horse.__proto__ === Horse.prototype);
console.log(Horse.prototype.__proto__ === Mammal.prototype);
console.log(Mammal.prototype.__proto__ === Animal.prototype);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment