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