g3.Class
test prototype/constructor
HTML
<span id="class"></span>
JavaScript
(function(window, document, undefined){
// Namespace object
var g3;
// Return as AMD module or attach to head object
if (typeof define !== 'undefined')
define([], function () {
g3 = g3 || {};
return g3;
});
else if (typeof window !== 'undefined')
g3 = window.g3 = window.g3 || {};
else{
g3 = g3 || {};
module.exports = g3;
}
g3.Class = function () {
var len = arguments.length;
if(len === 0 || (len === 1 && arguments[0] === null))
return function() {};
var body = arguments[len - 1],
SuperClass = len > 1 ? arguments[0] : null,
implementClasses = len > 2,
Class,
SuperClassEmpty,
i;
//we expect last object to override 'constructor' otherwise the new is empty!
if (body.constructor === Object) {
Class = function() {};
} else {
Class = body.constructor;
delete body.constructor;
}
//'Class.Super' is a reserved word for g3.Class!
if (SuperClass) {
SuperClassEmpty = function() {};
SuperClassEmpty.prototype = SuperClass.prototype;
Class.prototype = new SuperClassEmpty();
Class.prototype.constructor = Class;
//doesn't work!
//Class.prototype.constructor.name = Class.prototype.toString();
Class.Super = SuperClass;
extend(Class, SuperClass, false); //works for static members!
}
if (implementClasses)
for (i = 1; i < len - 1; i++)
if(typeof arguments[i] === 'object')
extend(Class.prototype, arguments[i], false, 'function');
else
extend(Class.prototype, arguments[i].prototype, false);
extendClass(Class, body);
return Class;
};
function extendClass(Class, extension, override) {
//'STATIC' is a reserved word from last argument of g3.Class!
if (extension.STATIC) {
extend(Class,...