[removed] Extend a Function

后端 未结 7 639
小鲜肉
小鲜肉 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:29

    The other methods are great but they don't preserve any prototype functions attached to init. To get around that you can do the following (inspired by the post from Nick Craver).

    (function () {
        var old_prototype = init.prototype;
        var old_init = init;
        init = function () {
            old_init.apply(this, arguments);
            // Do something extra
        };
        init.prototype = old_prototype;
    }) ();
    

提交回复
热议问题