Skip to content

Instantly share code, notes, and snippets.

@benjaminramey
Created February 1, 2012 15:53
Show Gist options
  • Save benjaminramey/1717674 to your computer and use it in GitHub Desktop.
Save benjaminramey/1717674 to your computer and use it in GitHub Desktop.
One way of extending prototypes in javascript to simulate class inheritance.
//
// '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