jsmoo
Version:
JavaScript Minimalist Object Orientation
41 lines (36 loc) • 1.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
// The before function importe to the global import of JSMOO
//
// object = The Ojbet on which the before funcition will search for the method to attach
// method = A string with the name of the method to attach
// func = The function which will be called before the method
function before(object, method, func) {
if (object.prototype) {
//TODO: FOR STATIC METHODS
//const oldFunction = object[method];
//object[method] = (...args) => {
//oldFunction.bind(object)(...args);
//beforeFunction.bind(object)(...args);
//};
} else {
if (!object._beforeFunctions_) object._beforeFunctions_ = [];
object._beforeFunctions_.push({ method: method, func: func });
}
}
// Function exported to the Jsmoo.js to attach the before functions to the new object
function defineBeforeFunctions() {
var _this = this;
if (!this._beforeFunctions_) return;
this._beforeFunctions_.forEach(function (override) {
var oldFunction = _this[override.method];
_this[override.method] = function () {
override.func.bind(_this).apply(undefined, arguments);
return oldFunction.bind(_this).apply(undefined, arguments);
};
});
}
exports.default = before;
exports.defineBeforeFunctions = defineBeforeFunctions;