0

I'd loike to know which solutions better to use module pattern or write a handle function for an input element action?

<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);

or

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

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

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

What is the better solution or there is another more better one, please?

1 Answer 1

1

I prefere a pattern like this...

'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));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.