What is the best practice to not to override other bound functions to [removed]?

后端 未结 3 826
星月不相逢
星月不相逢 2021-01-03 23:36

How much I dig into JavaScript, I find myself asking that much. For example we have window.onresize event handler and if I say:

window.onresize          


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-04 00:26

    One way of doing it is like this:

    function resize() { /* ... */ }
    
    var existing = window.onresize;
    window.onresize = function() {
        if (existing) {
            existing();
        }
        resize();
     }
    

    Or you can use something like jQuery which wraps all that stuff in a much simpler construct:

    $(window).resize(function() { /* ... */ });
    

    That automatically handles multiple handlers and stuff for you.

提交回复
热议问题