Skip to content

Instantly share code, notes, and snippets.

@FrankSpierings
Created October 16, 2018 18:45
Show Gist options
  • Save FrankSpierings/e79e87cc9f35230dc4e86d78d8ed0d4e to your computer and use it in GitHub Desktop.
Save FrankSpierings/e79e87cc9f35230dc4e86d78d8ed0d4e to your computer and use it in GitHub Desktop.
Javascript hooking a function
Function.prototype.clone = function() {
var that = this;
var name = this.name;
var hooked = function() {
console.trace('[' + name + '] Pre hook log: ' + JSON.stringify(arguments));
result = that.apply(this, arguments);
console.trace('[' + name + '] Post hook log: ' + JSON.stringify(result));
return result;
};
for(var key in this) {
if (this.hasOwnProperty(key)) {
hooked[key] = this[key];
}
}
return hooked;
};
function testing(msg) {
console.log('This is the message:' + msg);
}
testing('Main');
testing = testing.clone();
testing('Hooked');
// console.log($.get('https://jsonplaceholder.typicode.com/todos/1'));
// $.get = $.get.clone();
// console.log($.get('https://jsonplaceholder.typicode.com/todos/2'));
@jan-karel
Copy link

Me like

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment