Created
August 20, 2012 18:23
-
-
Save Gerjo/3406433 to your computer and use it in GitHub Desktop.
JavaScript prototype inheritance.
This file contains hidden or 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.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