Skip to content

Instantly share code, notes, and snippets.

@zzzhc
Created December 17, 2012 05:48
Show Gist options
  • Save zzzhc/4316055 to your computer and use it in GitHub Desktop.
Save zzzhc/4316055 to your computer and use it in GitHub Desktop.
javascript prototype chain demonstration
var Creature = function() {};
Creature.prototype = {
f1 : function() {
console.log("f1 in creature");
}
};
var Animal = function() {};
Animal.prototype = new Creature;
Animal.prototype.constructor = Animal;
Animal.prototype.f2 = function() {
console.log("f2 in animal");
};
var Dog = function() {};
Dog.prototype = new Animal;
Dog.prototype.constructor = Dog;
Dog.prototype.f3 = function() {
console.log("f3 in dog");
};
dog = new Dog;
dog.f1();
dog.f2();
dog.f3();
Creature.prototype.f11 = function() {
console.log("f11 in creature");
};
dog.f11();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment