Created
June 22, 2013 15:49
-
-
Save prettymuchbryce/5841350 to your computer and use it in GitHub Desktop.
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
//Extend function | |
Function.prototype.extend = function(parent) { | |
this.prototype = Object.create(parent.prototype); | |
this.prototype.constructor = this; | |
return this; | |
}; | |
//Base Class | |
var BaseClass = function() {}; | |
BaseClass.prototype.sharedMethod = function() { | |
console.log("This method is shared"); | |
}; | |
BaseClass.prototype.baseMethod = function() { | |
console.log("I am the base method."); | |
}; | |
//Child Class | |
var ChildClass = function() {}; | |
ChildClass.extend(BaseClass); | |
ChildClass.prototype.baseMethod = function() { | |
console.log("I am the overridden method."); | |
}; | |
//Implementation | |
var baseClass = new BaseClass(); | |
var childClass = new ChildClass(); | |
baseClass.sharedMethod(); //"This method is shared" | |
childClass.sharedMethod(); //"This method is shared" | |
baseClass.baseMethod(); //"I am the base method" | |
childClass.baseMethod(); //"I am the overridden method" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment