Dollar sign before self declaring anonymous function in JavaScript?

前端 未结 5 1528
闹比i
闹比i 2020-11-29 03:08

What is the difference between these two:

$(function () {
    // do stuff
});

AND

(function () {
    // do stuff
})();
         


        
5条回答
  •  一个人的身影
    2020-11-29 03:42

    $(function () {
        // It will invoked after document is ready
    });
    

    This function execution once documents get ready mean, the whole HTML should get loaded before its execution but in the second case, the function invoked instantly after it is created.

    (function () {
        // It will invoked instantly after it is created
    })();
    

提交回复
热议问题