Last active
September 1, 2020 21:45
-
-
Save petsel/895cd9eb04239e10e8eb to your computer and use it in GitHub Desktop.
possible implementation of prototypal method modifier [around].
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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