Created
February 1, 2012 15:53
-
-
Save benjaminramey/1717674 to your computer and use it in GitHub Desktop.
One way of extending prototypes in javascript to simulate class 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
// | |
// 'extend' code from Mozilla | |
// https://developer.mozilla.org/En/Core_JavaScript_1.5_Guide/Inheritance | |
// Used to set up inheritance between JS types. | |
// | |
extend: function(childtype, supertype) | |
{ | |
for (var property in supertype.prototype) | |
{ | |
if (typeof childtype.prototype[property] == "undefined") | |
childtype.prototype[property] = supertype.prototype[property]; | |
} | |
// so you can call this.__super.prototype.METHOD_NAME.apply(this, args[]); | |
// to call overridden superclass methods | |
// this.__super.call(this, arg1, arg2, ...) calls the parent constructor | |
childtype.prototype.__super = supertype; | |
return childtype; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment