Saturday, June 1, 2024
 Popular · Latest · Hot · Upcoming
153
rated 0 times [  160] [ 7]  / answers: 1 / hits: 28235  / 14 Years ago, mon, march 7, 2011, 12:00:00

Can I override the behavior of the Function object so that I can inject behavior prior t every function call, and then carry on as normal? Specifically, (though the general idea is intriguing in itself) can I log to the console every function call without having to insert console.log statements everywhere? And then the normal behavior goes on?



I do recognize that this will likely have significant performance problems; I have no intention of having this run typically, even in my development environment. But if it works it seems an elegant solution to get a 1000 meter view on the running code. And I suspect that the answer will show me something deeper about javascript.


More From » javascript

 Answers
54

The obvious answer is something like the following:



var origCall = Function.prototype.call;
Function.prototype.call = function (thisArg) {
console.log(calling a function);

var args = Array.prototype.slice.call(arguments, 1);
origCall.apply(thisArg, args);
};


But this actually immediately enters an infinite loop, because the very act of calling console.log executes a function call, which calls console.log, which executes a function call, which calls console.log, which...



Point being, I'm not sure this is possible.


[#93386] Saturday, March 5, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
myakylas

Total Points: 66
Total Questions: 85
Total Answers: 95

Location: Guadeloupe
Member since Sat, Aug 22, 2020
4 Years ago
myakylas questions
Thu, Apr 28, 22, 00:00, 2 Years ago
Thu, Apr 8, 21, 00:00, 3 Years ago
Sat, Sep 19, 20, 00:00, 4 Years ago
;