Can I intercept a function called directly?

后端 未结 4 522
谎友^
谎友^ 2020-12-05 07:27

In this code I created a function called someFunction. Then I modified Function.prototype.apply and call methods. So instead of my function code is working I am running my i

4条回答
  •  鱼传尺愫
    2020-12-05 07:56

    There is a chance you can intercept direct function call. This requires:

    • Either the function is created by Function.prototype.bind and you have to overwrite Function.prototype.bind before creating the function, or
    • The function is created from Function() (or new Function()) and you also have to overwrite Function function before creating the target function.

    If neither of the above two can be met, the only way to intercept a direct call is to wrap the target function, which is the solution provided by AndyE https://stackoverflow.com/a/3406523/1316480

    For a function that is created by function literal and is hidden in private scope, there is no way to intercept a direct call to it.

    I have a blog post concludes all of these: http://nealxyc.wordpress.com/2013/11/25/intercepting-javascript-function/

提交回复
热议问题