loopback-datasource-juggler/lib/jutil.js

68 lines
1.9 KiB
JavaScript
Raw Normal View History

2013-05-29 17:03:01 +00:00
var util = require('util');
2013-05-28 05:20:30 +00:00
/**
*
* @param newClass
* @param baseClass
*/
2013-05-29 17:03:01 +00:00
exports.inherits = function (newClass, baseClass, options) {
util.inherits(newClass, baseClass);
options = options || {
staticProperties: 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);
}
});
}
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,
2013-05-28 20:50:59 +00:00
override: false
2013-05-28 05:20:30 +00:00
};
if (options.staticProperties) {
Object.keys(baseClass).forEach(function (classProp) {
if (classProp !== 'super_' && (!newClass.hasOwnProperty(classProp) || options.override)) {
2013-05-28 20:50:59 +00:00
var pd = Object.getOwnPropertyDescriptor(baseClass, classProp);
Object.defineProperty(newClass, classProp, pd);
2013-05-28 05:20:30 +00:00
}
});
}
if (options.instanceProperties) {
if (baseClass.prototype) {
Object.keys(baseClass.prototype).forEach(function (instanceProp) {
2013-05-28 20:50:59 +00:00
if (!newClass.hasOwnProperty(instanceProp) || options.override) {
var pd = Object.getOwnPropertyDescriptor(baseClass.prototype, instanceProp);
Object.defineProperty(newClass.prototype, instanceProp, pd);
}
2013-05-28 05:20:30 +00:00
});
}
}
if (Array.isArray(newClass._mixins)) {
newClass._mixins.push(baseClass);
} else {
newClass._mixins = [baseClass];
}
2013-05-28 20:50:59 +00:00
return newClass;
2013-05-28 05:20:30 +00:00
};