loopback-datasource-juggler/lib/jutil.js

52 lines
1.4 KiB
JavaScript
Raw Normal View History

2013-05-28 05:20:30 +00:00
/**
*
* @param newClass
* @param baseClass
*/
2011-10-10 13:22:51 +00:00
exports.inherits = function (newClass, baseClass) {
2013-05-28 05:20:30 +00:00
Object.keys(baseClass).forEach(function (classProp) {
newClass[classProp] = baseClass[classProp];
2011-10-10 13:22:51 +00:00
});
2013-05-28 05:20:30 +00:00
Object.keys(baseClass.prototype).forEach(function (instanceProp) {
newClass.prototype[instanceProp] = baseClass.prototype[instanceProp];
2011-10-10 13:22:51 +00:00
});
};
2013-05-28 05:20:30 +00:00
/**
* 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: true
};
if (options.staticProperties) {
Object.keys(baseClass).forEach(function (classProp) {
if (classProp !== 'super_' && (!newClass.hasOwnProperty(classProp) || options.override)) {
newClass[classProp] = baseClass[classProp];
}
});
}
if (options.instanceProperties) {
if (baseClass.prototype) {
Object.keys(baseClass.prototype).forEach(function (instanceProp) {
newClass.prototype[instanceProp] = baseClass.prototype[instanceProp];
});
}
}
if (Array.isArray(newClass._mixins)) {
newClass._mixins.push(baseClass);
} else {
newClass._mixins = [baseClass];
}
};