Created
May 16, 2013 10:36
-
-
Save jedrichards/5590832 to your computer and use it in GitHub Desktop.
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.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