Inject javascript into a javascript function

前端 未结 7 1787
别跟我提以往
别跟我提以往 2021-02-04 13:25

I\'ve got a weird question in that I need to inject some javascript into another javascript function. I am using a framework which is locked so I can not change the existing fun

7条回答
  •  礼貌的吻别
    2021-02-04 14:05

    Functions are first class values in Javascript, which means that you can store them in variables (and in fact, declaring a named function is essentially assigning an anonymous function to a variable).

    You should be able to do something like this:

    function doSomething() { ... }
    
    var oldVersionOfFunc = doSomething;
    doSomething = function() {
    
        var retVal = oldVersionOfFunc.apply(oldVersionOfFunc, args);
    
        // your added code here
    
        return retVal;
    }
    

    (But, I could be wrong. My javascript's a little rusty. ;))

提交回复
热议问题