Skip to content

Instantly share code, notes, and snippets.

@petsel
Last active September 1, 2020 21:45
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/895cd9eb04239e10e8eb to your computer and use it in GitHub Desktop.
Save petsel/895cd9eb04239e10e8eb to your computer and use it in GitHub Desktop.
possible implementation of prototypal method modifier [around].
(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 around/*Modifier*/(handler, target) {
target = getSanitizedTarget(target);
const proceed = this;
return (
isFunction(handler) &&
isFunction(proceed) &&
function () {
return handler.call((target || getSanitizedTarget(this)), proceed, handler, arguments);
}
) || proceed;
}
// around.toString = () => 'around() { [native code] }';
Object.defineProperty(fctPrototype, 'around', {
configurable: true,
writable: true,
value: around/*Modifier*/
});
// provide static implementation as well.
function staticAround/*Modifier*/(proceed, handler, target) {
return around.call(proceed, handler, target);
}
// staticAround.toString = () => 'around() { [native code] }';
// staticAround.toString = () => 'staticAround() { [native code] }';
Object.defineProperty(Function, 'around', {
configurable: true,
writable: true,
value: staticAround/*Modifier*/
});
}(Function));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment