Skip to content

Instantly share code, notes, and snippets.

@prettymuchbryce
Created June 22, 2013 15:49
Show Gist options
  • Save prettymuchbryce/5841350 to your computer and use it in GitHub Desktop.
Save prettymuchbryce/5841350 to your computer and use it in GitHub Desktop.
//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