Skip to content

Instantly share code, notes, and snippets.

@SidIcarus
Created January 29, 2020 22:55
Show Gist options
  • Save SidIcarus/a0c0a47a7a614492d61ea3133d7cf231 to your computer and use it in GitHub Desktop.
Save SidIcarus/a0c0a47a7a614492d61ea3133d7cf231 to your computer and use it in GitHub Desktop.
jQuery log function decorator
function jqueryLogDecoratorInstaller () {
console.log('Decorating jQuery methods with logging');
const install = (name, oldMethod) => {
$.fn[name] = function () { // cannot be shorthand
console.log(`${this.selector}.${name}(${Array.from(arguments).toString()})`);
return oldMethod.apply(this, arguments);
};
};
let property, method;
for (property in $.fn) {
method = $.fn[property];
if (
Object.prototype.hasOwnProperty.call($.fn, property) &&
$.isFunction(method) &&
property !== 'init'
) {
install(property, method);
}
}
}
jqueryLogDecoratorInstaller();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment