2017-07-24 20 views
0

我想知道哪些解决方案更适合使用模块模式或为输入元素操作编写句柄函数?JavaScript模块模式函数或jQuery代理

<button type="button" onClick="main.update.buttonClick">Click</button> 

(function($) { 
'use strict'; 
window.main.update = window.main.update ||(); 

main.update.init = function() { ... }; 

main.update.buttonClick = function() { ... }; })(jQuery); 

<button id="update" type="button">Click</button> 

(function($) { 
'use strict'; 
window.main.update = window.main.update ||(); 

main.update.init = function() { 
    $("#update").on("click", functio() { ... }); 
}; 

什么是更好的解决方案,或有其它更多更好的吗?

回答

1

我prefere的评论这样的模式...

'use strict'; 

(function ($) { 

    function doStuffHere() { 
     // Do stuff here 
    } 

    function init() { 
     $(window).on('whatever', function() { 
      // Trigger the above on what you want and call function 
      function doStuffHere(); 
     } 
    } 

    // This shorthand for call function init() on dom ready 
    $(init); 

}(jQuery)); 
+0

感谢。 – sada