javascriptjqueryclosuresaddeventlistenerself-invoking-function

Self Invoking Function as jQuery ducument ready callback


What is the difference between

$(function() {
    // bind some event listeners
});

and

$(function() {
    // bind some event listeners
}());

Solution

  • $(function() {
        // bind some event listeners
    });
    

    In above case the function is passed to the jquery which will get executed once document is ready.

    $(function() {
        // bind some event listeners
    }());
    

    In above case the return of the function is passed to the jquery. Since the function is se3lf executing itself, it will get executed immediately and whatever the function returns will get passed to the jquery, so this is not a good way because the objective is to execute the function once document gets ready which is not happening in this case