Created
January 29, 2020 22:55
-
-
Save SidIcarus/a0c0a47a7a614492d61ea3133d7cf231 to your computer and use it in GitHub Desktop.
jQuery log function decorator
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 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