The OP's approach is the most intuitive/natural one. With JavaScript applications one sometimes is in need of intercepting and/or modifying the control flow of functionality one does not own or is, for other reasons, not allowed to touch.
For exactly this scenario there is no other way than to preserve and alter such logic by wrapping their original implementation. This ability is not unique to JavaScript. There is quite a history of programming languages that enable Metaprogramming via Reflection and Self-Modification.
Of cause one could/should provide bulletproof but handy abstractions for all the possibly modifier use cases that one can think of ... starting right away with the OP's use case that is the most obvious and easiest to implement one, which could be handled by e.g a before modifier ...
const obj = {
shout: function (...args) {
console.log('AHHHHH!');
console.log('args : ', args);
console.log('this is me : ', this);
}
};
obj.shout();
obj.shout = obj.shout.before(function () {
console.log("I'm going to shout.");
}, obj);
obj.shout('test of "before" modifier');
const anotherOne = {
name: 'anotherOne'
};
obj.shout.call(anotherOne, 'delegation and self reflection');
/*
console.log(
'Object.getOwnPropertyDescriptors(Function.prototype) :',
Object.getOwnPropertyDescriptors(Function.prototype)
);
*/
.as-console-wrapper { min-height: 100%!important; top: 0; }
<script>
(function (Function) {
const fctPrototype = Function.prototype;
const FUNCTION_TYPE = (typeof Function);
function isFunction(type) {
return (
(typeof type == FUNCTION_TYPE)
&& (typeof type.call == FUNCTION_TYPE)
&& (typeof type.apply == FUNCTION_TYPE)
);
}
function getSanitizedTarget(target) {
return ((target != null) && target) || null;
}
function before/*Modifier*/(handler, target) {
target = getSanitizedTarget(target);
const proceed = this;
return (
isFunction(handler) &&
isFunction(proceed) &&
function () {
const context = target || getSanitizedTarget(this);
const args = arguments;
//handler.apply(context, args);
handler.call(context, args);
return proceed.apply(context, args);
}
) || proceed;
}
// before.toString = () => 'before() { [native code] }';
Object.defineProperty(fctPrototype, 'before', {
configurable: true,
writable: true,
value: before/*Modifier*/
});
}(Function));
</script>
As one can see from the example code, which uses the additionally provided implementation, Function.prototype
is quite comfortable for enabling method modifiers in JavaScript.
But regardless of how/where one does implement such modifier functionality, following applies to any reliable modifier code (Bergi already did point to it) ...
- The modifier has to support a
target
object (for delegating the correct this
context, if needed).
- The modifier has to either correctly forward a wrapped method's arguments or handle its return value respectively the exception of its failing invocation.
I wouldn't mind if, at one day, JavaScript officially features ... Function.prototype[
before
|
after
|
around
|
afterThrowing
|
afterFinally
]
.