What does (function($) {})(jQuery); mean?

前端 未结 6 868
灰色年华
灰色年华 2020-11-22 08:38

I am just starting out with writing jQuery plugins. I wrote three small plugins but I have been simply copying the line into all my plugins without actually knowing what it

6条回答
  •  余生分开走
    2020-11-22 09:38

    Just small addition to explanation

    This structure (function() {})(); is called IIFE (Immediately Invoked Function Expression), it will be executed immediately, when the interpreter will reach this line. So when you're writing these rows:

    (function($) {
      // do something
    })(jQuery);
    

    this means, that the interpreter will invoke the function immediately, and will pass jQuery as a parameter, which will be used inside the function as $.

提交回复
热议问题