Can I intercept a function called directly?

后端 未结 4 525
谎友^
谎友^ 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 08:05

    Function.prototype.callWithIntercept = function () {
        alert("intercept");
        return this.apply(null, arguments);
    };
    

    var num = parseInt.callWithIntercept("100px", 10);
    

    It is worth noting that in newer versions of JS, there are Proxy objects you can use: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy

提交回复
热议问题