Dollar sign before self declaring anonymous function in JavaScript?

前端 未结 5 1522
闹比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:26

    $(function() {}); is a jQuery shortcut for

     $(document).ready(function() { 
         /* Handler for .ready() called. */ 
     });
    

    While (function() {})(); is a instantly invoked function expression, or IIFE. This means that its an expression (not a statement) and it is invoked instantly after it is created.

提交回复
热议问题