Skip to content

Instantly share code, notes, and snippets.

@Gerjo
Created August 20, 2012 18:23
Show Gist options
  • Save Gerjo/3406433 to your computer and use it in GitHub Desktop.
Save Gerjo/3406433 to your computer and use it in GitHub Desktop.
JavaScript prototype inheritance.
function Animal(name) {
this.ssname = name;
console.log("Animal ctor", arguments);
}
Animal.prototype.walk = function() {
console.log("Animal walks", this.ssname);
}
Cat.prototype = new Animal();
Cat.prototype.constructor = Cat;
function Cat(name){
Animal.apply(this, arguments);
console.log("Cat ctor", arguments);
}
Cat.prototype.walk = function() {
console.log("Cat walks", this.ssname);
Animal.prototype.walk.call(this);
}
var a = new Cat("darwin");
var b = new Cat("james");
a.walk();
b.walk();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment