|
'use strict'; |
|
|
|
Tigerian.MainClassDefinition = function () {}; |
|
|
|
/** |
|
* @param {Object} properties |
|
* @param {Function} superClass |
|
* @param {Behavior[]} behaviors |
|
*/ |
|
Tigerian.MainClassDefinition.extend = function (properties, behaviors, superClass) { |
|
if (!Tigerian.MainClassDefinition.isInstance(properties, "object")) { |
|
properties = {}; |
|
} |
|
if (!Tigerian.MainClassDefinition.isInstance(superClass, Function)) { |
|
superClass = Tigerian.MainClassDefinition; |
|
} |
|
if (!Tigerian.MainClassDefinition.isInstance(behaviors, Array)) { |
|
behaviors = []; |
|
} |
|
|
|
var result = function () { |
|
Object.defineProperty(this, "super", { |
|
enumerable: false, |
|
configurable: true, |
|
writable: false, |
|
value: function () { |
|
return superClass.apply(this, Array.from(arguments)); |
|
}, |
|
}); |
|
|
|
if ("init" in properties) { |
|
properties["init"].apply(this, Array.from(arguments)); |
|
} else { |
|
superClass.apply(this, Array.from(arguments)); |
|
} |
|
delete this.super; |
|
|
|
for (var bhv in behaviors) { |
|
if (MainClassDefinition.isSubclass(behaviors[bhv], Behavior)) { |
|
var bhvCls = behaviors[bhv]; |
|
bhvCls.call(this); |
|
} |
|
} |
|
|
|
for (var prop in properties) { |
|
if (prop !== "init") { |
|
this[prop] = properties[prop]; |
|
} |
|
} |
|
}; |
|
|
|
/** |
|
* @param {Object} props |
|
* @param {Behavior[]} behavs |
|
*/ |
|
result.extend = function (props, behavs) { |
|
if (!MainClassDefinition.isInstance(behavs, Array)) { |
|
behavs = Array.from(arguments).splice(1); |
|
} |
|
return MainClassDefinition.extend(props, behaviors.concat(behavs), result); |
|
}; |
|
|
|
result.prototype = Object.create(superClass.prototype); |
|
result.prototype.constructor = result; |
|
|
|
return result; |
|
}; |
|
|
|
/** |
|
* @param {Function} subClass |
|
* @param {Function} superClass |
|
*/ |
|
MainClassDefinition.isSubclass = function (subClass, superClass) { |
|
if ((subClass instanceof Function) && (superClass instanceof Function)) { |
|
return (Object.create(subClass.prototype) instanceof superClass); |
|
} |
|
}; |
|
|
|
/** |
|
* @param {*} instance |
|
* @param {string|Function} type |
|
* @returns {boolean} |
|
*/ |
|
MainClassDefinition.isInstance = function (instance, type) { |
|
if (typeof type === "string") { |
|
return (typeof instance === type); |
|
} else if (typeof type === "function") { |
|
var result = (instance instanceof type); |
|
|
|
return result; |
|
} else { |
|
return false; |
|
} |
|
}; |
|
|
|
|
|
Class = MainClassDefinition.extend(); |
|
Class.isSubclass = MainClassDefinition.isSubclass; |
|
Class.isInstance = MainClassDefinition.isInstance; |