Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@petsel
Last active September 1, 2020 21:36
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/37bf42a6fc0b7f9549b8 to your computer and use it in GitHub Desktop.
Save petsel/37bf42a6fc0b7f9549b8 to your computer and use it in GitHub Desktop.
possible implementation of prototypal method modifier [before].
(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