Can I intercept a function called directly?

后端 未结 4 527
谎友^
谎友^ 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:59

    You can only override a known function by setting another function in its place (e.g., you can't intercept ALL function calls):

    (function () {
        // An anonymous function wrapper helps you keep oldSomeFunction private
        var oldSomeFunction = someFunction;
    
        someFunction = function () {
            alert("intercepted!");
            oldSomeFunction();
        }
    })();
    

    Note that, if someFunction was already aliased/referenced by another script before it was changed by this code, those references would still point to the original function not be overridden by the replacement function.

提交回复
热议问题