Skip to content

Instantly share code, notes, and snippets.

@jedrichards
Created May 16, 2013 10:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedrichards/5590832 to your computer and use it in GitHub Desktop.
Save jedrichards/5590832 to your computer and use it in GitHub Desktop.
// extend.js
define(function (require) {
var _ = require("vendor/underscore");
var ctor = function () {};
var inherits = function (parent,protoProps,staticProps) {
var child;
if (protoProps && protoProps.hasOwnProperty("constructor")) {
child = protoProps.constructor;
} else {
child = function () { return parent.apply(this,arguments); };
}
_.extend(child,parent);
ctor.prototype = parent.prototype;
child.prototype = new ctor();
if (protoProps) _.extend(child.prototype,protoProps);
if (staticProps) _.extend(child,staticProps);
child.prototype.constructor = child;
child.__super__ = parent.prototype;
return child;
};
function extendThis (protoProps,staticProps) {
var child = inherits(this,protoProps,staticProps);
child.extend = extendThis;
return child;
}
return extendThis.bind(extendThis);
});
// Usage
define(function (require) {
var extend = require("extend");
var MyClass = extend({
init: function (options) {
console.log("MyClass.init");
}
});
var myClass = new MyClass();
myClass.init({}); // MyClass.init
var MySubClass = MyClass.extend({
init: function (options) {
MyClass.prototype.init.call(this,options);
console.log("MySubClass.init");
}
});
var mySubClass = new MySubClass();
mySubClass.init(); // MyClass.init + MySubClass.init
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment