Append code to the end of an existing function

后端 未结 5 1979
别那么骄傲
别那么骄傲 2021-02-04 03:17

I need to trigger function bar() whenever function foo() fires. I have no control over function foo or whether it will change in the future. I have this situation regularly (and

5条回答
  •  后悔当初
    2021-02-04 03:56

    You can just override foo with a custom function that calls the original.

    E.g.

    var old_foo = foo;
    foo = function() {
      old_foo();
      bar();
    }
    

    You should also pass any arguments foo takes into it via your replacement function.

提交回复
热议问题