How can I add JS code that dynamically wrap any function invocation?

730 Views Asked by At

I had like to wrap any JavaScript invocation at runtime, e.g. I had like to write to a log that an invocation of Func has been occurred.

This wrapping must work for any function even those function that has been added using eval or prototyping.

2

There are 2 best solutions below

0
On BEST ANSWER

What your looking for is node-proxy

You can't do this using native JS. This will only work for node.js. It can probably be adjusted to work for any js running on V8.

1
On

If you were to call your functions with the call method, you could do something like this:

oldCall = Function.prototype.call;
Function.prototype.call = function(){
 // do some logging here
 oldCall.apply(this, arguments);
}