the problem is very simple to understand yet tricky to implement.
if i have a predefined function(not one that i make) console.log("hello")
. I would like to modify console.log()
so that when i run console.log()
anywhere, i can run a function where i get the arguments make some changes, example "hello" becomes "user1: hello".
The only way i can think of is:
Function MyOwnConsoleLog(string){
string = "test.. " + string;
console.log(string);
};
console.log = myCustomConsoleLogFunction
console.log("hello") //almost works!
This almost works but as you see console.log inside MyOwnConsoleLog is no longer console.log() XD which means that it will recurse infinitely and fail
Is it possible to make this work, maybe with some console.log.bind()/.apply()/.xxx or something else?
btw I have tried saving the old console.log()
to another variable, but it seems pretty impossible to make a copy of this function, have tried alot of deep copies and other methods here at stackoverflow, none of them managed to make a fully working console.log() independent copy.