Created
March 2, 2012 17:23
-
-
Save cowboy/1959794 to your computer and use it in GitHub Desktop.
JavaScript: Prototypal Inheritance, Extend
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
// Also see http://michaux.ca/articles/class-based-inheritance-in-javascript | |
function extend(Super, Sub) { | |
// By using a dummy constructor, initialization side-effects are eliminated. | |
function Dummy() {} | |
// Set dummy prototype to Super prototype. | |
Dummy.prototype = Super.prototype; | |
// Create Sub prototype as a new instance of dummy. | |
Sub.prototype = new Dummy(); | |
// The .constructor propery is really the Super constructor. | |
Sub.baseConstructor = Sub.prototype.constructor; | |
// Update the .constructor property to point to the Sub constructor. | |
Sub.prototype.constructor = Sub; | |
// A convenient reference to the super constructor. | |
Sub.superConstructor = Super; | |
// A convenient reference to the super prototype. | |
Sub.superPrototype = Super.prototype; | |
} | |
// THE BASE TYPE | |
function Mammal(name) { | |
this.name = name; | |
Mammal.population++; | |
} | |
Mammal.population = 0; | |
Mammal.prototype.toString = function() { | |
return "Mammal " + this.name; | |
}; | |
Mammal.prototype.giveBirth = function() { | |
return new this.constructor("Baby " + this.name); | |
}; | |
var cat = new Mammal("Cat"); | |
console.log("Cat #1: " + cat); | |
var kitten = cat.giveBirth(); | |
console.log("Kitten #1: " + kitten); | |
console.log("Population: " + Mammal.population); | |
console.log("-----------"); | |
// ONE LEVEL OF INHERITANCE | |
function Cat(name) { | |
Cat.superConstructor.call(this, name); | |
} | |
extend(Mammal, Cat); | |
Cat.prototype.toString = function() { | |
return "Cat " + this.name; | |
}; | |
Cat.prototype.meow = function() { | |
return ">> MEOW <<"; | |
}; | |
Cat.prototype.giveBirth = function() { | |
var kitten = Cat.superPrototype.giveBirth.call(this); | |
console.log(kitten.meow()); | |
return kitten; | |
}; | |
var cat = new Cat("Fluffy"); | |
console.log("Cat #2: " + cat); | |
var kitten = cat.giveBirth(); | |
console.log("Kitten #2: " + kitten); | |
console.log("Population: " + Mammal.population); | |
console.log("-----------"); | |
// TWO LEVELS OF INHERITANCE | |
function ShowCat(name, breed) { | |
ShowCat.superConstructor.call(this, name); | |
this.breed = breed; | |
} | |
extend(Cat, ShowCat); | |
ShowCat.prototype.toString = function() { | |
return "Fancy " + ShowCat.superPrototype.toString.call(this);; | |
}; | |
ShowCat.prototype.meow = function() { | |
return ShowCat.superPrototype.meow.call(this).replace(" ", " FANCY "); | |
}; | |
var cat = new ShowCat("Fluffy"); | |
console.log("Cat #3: " + cat); | |
var kitten = cat.giveBirth(); | |
console.log("Kitten #3: " + kitten); | |
console.log("Population: " + Mammal.population); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment