Last active
December 31, 2015 00:09
-
-
Save kylefeng/7905641 to your computer and use it in GitHub Desktop.
prototype chain
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 = { | |
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