[removed] Extend a Function

后端 未结 7 657
小鲜肉
小鲜肉 2020-11-30 17:47

The main reason why I want it is that I want to extend my initialize function.

Something like this:

// main.js

window.onload = init();
function init         


        
7条回答
  •  生来不讨喜
    2020-11-30 18:40

    There are several ways to go about this, it depends what your purpose is, if you just want to execute the function as well and in the same context, you can use .apply():

    function init(){
      doSomething();
    }
    function myFunc(){
      init.apply(this, arguments);
      doSomethingHereToo();
    }
    

    If you want to replace it with a newer init, it'd look like this:

    function init(){
      doSomething();
    }
    //anytime later
    var old_init = init;
    init = function() {
      old_init.apply(this, arguments);
      doSomethingHereToo();
    };
    

提交回复
热议问题