What is the difference between these two javascript functions?

I watched some jQuery plugins work, and I saw how the next action was closed around the whole plugin.

$(function(){
    // plugin code here

});

(function($){
    // plugin code here

})(jQuery);

What is the difference between the two?

+3
source share
2 answers

The first is the definition of the JQuery DOM Ready function, and the second is the closure of the function, which is performed immediately after its analysis and is performed with the jQueryobject as a parameter.

, - , ( ).
, , , .

+8

- jQuery.ready. , , DOM ( script, , ) — , CSS, .. ..

( , DOM ) jQuery $. , $ jQuery, , jQuery.noConflict $ , (, Prototype) jQuery. , , $, , Prototype - , $. .

, :

jQuery(function($) {
    // Code that uses `$` and expects the DOM to be ready to be
    // manipulated goes here
});
+3

Source: https://habr.com/ru/post/1745954/


All Articles