Created
October 16, 2018 18:45
-
-
Save FrankSpierings/e79e87cc9f35230dc4e86d78d8ed0d4e to your computer and use it in GitHub Desktop.
Javascript hooking a function
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.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')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Me like