Skip to content

Instantly share code, notes, and snippets.

@petsel
Last active September 1, 2020 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petsel/9ddf5ce515f1344c84bd to your computer and use it in GitHub Desktop.
Save petsel/9ddf5ce515f1344c84bd to your computer and use it in GitHub Desktop.
possible implementation of prototypal method modifier [afterThrowing].
(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) {
/* eslint-disable */
/* jshint ignore:start */
/* ignore jslint start */
/* no-eq-null */
return ((target != null) && target) || null;
/* ignore jslint end */
/* jshint ignore:end */
/* eslint-enable */
}
function afterThrowing/*Modifier*/(handler, target) {
target = getSanitizedTarget(target);
const proceed = this;
return (
isFunction(handler) &&
isFunction(proceed) &&
function () {
const context = target || getSanitizedTarget(this);
const args = arguments;
let result;
try {
result = proceed.apply(context, args);
} catch (exception) {
result = handler.call(context, exception, args);
}
return result;
}
) || proceed;
}
// afterThrowing.toString = () => 'afterThrowing() { [native code] }';
Object.defineProperty(fctPrototype, 'afterThrowing', {
configurable: true,
writable: true,
value: afterThrowing/*Modifier*/
});
// provide static implementation as well.
function staticAfterThrowing/*Modifier*/(proceed, handler, target) {
return afterThrowing.call(proceed, handler, target);
}
// staticAfterThrowing.toString = () => 'afterThrowing() { [native code] }';
// staticAfterThrowing.toString = () => 'staticAfterThrowing() { [native code] }';
Object.defineProperty(Function, 'afterThrowing', {
configurable: true,
writable: true,
value: staticAfterThrowing/*Modifier*/
});
}(Function));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment