[removed] Extend a Function

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

    Use extendFunction.js

    init = extendFunction(init, function(args) {
      doSomethingHereToo();
    });
    

    But in your specific case, it's easier to extend the global onload function:

    extendFunction('onload', function(args) {
      doSomethingHereToo();
    });
    

    I actually really like your question, it's making me think about different use cases.

    For javascript events, you really want to add and remove handlers - but for extendFunction, how could you later remove functionality? I could easily add a .revert method to extended functions, so init = init.revert() would return the original function. Obviously this could lead to some pretty bad code, but perhaps it lets you get something done without touching a foreign part of the codebase.

    0 讨论(0)
提交回复
热议问题