Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Self Invoking Function as jQuery ducument ready callback

What is the difference between

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

and

$(function() {
    // bind some event listeners
}());
like image 454
Aram810 Avatar asked Apr 13 '17 09:04

Aram810


1 Answers

$(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

like image 162
Ice Box Avatar answered Sep 21 '22 18:09

Ice Box