58 lines
1.6 KiB
JavaScript
58 lines
1.6 KiB
JavaScript
/**
|
|
*
|
|
* @param newClass
|
|
* @param baseClass
|
|
*/
|
|
exports.inherits = function (newClass, baseClass) {
|
|
Object.keys(baseClass).forEach(function (classProp) {
|
|
newClass[classProp] = baseClass[classProp];
|
|
});
|
|
Object.keys(baseClass.prototype).forEach(function (instanceProp) {
|
|
newClass.prototype[instanceProp] = baseClass.prototype[instanceProp];
|
|
});
|
|
};
|
|
|
|
|
|
/**
|
|
* Mix in the base class into the new class
|
|
* @param newClass
|
|
* @param baseClass
|
|
* @param options
|
|
*/
|
|
exports.mixin = function (newClass, baseClass, options) {
|
|
options = options || {
|
|
staticProperties: true,
|
|
instanceProperties: true,
|
|
override: false
|
|
};
|
|
|
|
if (options.staticProperties) {
|
|
Object.keys(baseClass).forEach(function (classProp) {
|
|
if (classProp !== 'super_' && (!newClass.hasOwnProperty(classProp) || options.override)) {
|
|
var pd = Object.getOwnPropertyDescriptor(baseClass, classProp);
|
|
Object.defineProperty(newClass, classProp, pd);
|
|
}
|
|
});
|
|
}
|
|
|
|
if (options.instanceProperties) {
|
|
if (baseClass.prototype) {
|
|
Object.keys(baseClass.prototype).forEach(function (instanceProp) {
|
|
if (!newClass.hasOwnProperty(instanceProp) || options.override) {
|
|
var pd = Object.getOwnPropertyDescriptor(baseClass.prototype, instanceProp);
|
|
Object.defineProperty(newClass.prototype, instanceProp, pd);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
if (Array.isArray(newClass._mixins)) {
|
|
newClass._mixins.push(baseClass);
|
|
} else {
|
|
newClass._mixins = [baseClass];
|
|
}
|
|
|
|
return newClass;
|
|
};
|
|
|