Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// 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
You can’t perform that action at this time.