Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between (function(){/*.....*/})(); and (function($){/*.....*/})(jQuery);

Is there difference between :

(function() {
     /*..........*/
})();

and :

(function($) {
     /*..........*/
})(jQuery);
like image 372
Ahmed Avatar asked Mar 20 '16 05:03

Ahmed


People also ask

What is the difference between using $( function and function?

Functional is different from function. A function is a mathematical machine which accepts one or more numbers as inputs and provides a number as an output. A functional is that accepts one or more functions as inputs and produces a number as an output. So, a Functional is a function of Functions.

What does $( document .ready function () mean?

$( document ). ready()A page can't be manipulated safely until the document is "ready." jQuery detects this state of readiness for you. Code included inside $( document ). ready() will only run once the page Document Object Model (DOM) is ready for JavaScript code to execute.

What does $() mean in JavaScript?

The $() function The dollar function, $(), can be used as shorthand for the getElementById function. To refer to an element in the Document Object Model (DOM) of an HTML page, the usual function identifying an element is: document.


1 Answers

Other people explained what the difference is, but not why you use the latter.

The $ variable is most often used by jQuery. If you have one script tag that loads jQuery and another that loads your code, it's perfectly fine. Now throw prototype.js into the mix. If you load prototype.js and then jQuery, $ will still be jQuery. Do it the other way around and now $ is prototype.js.

If you tried to use $ on such a page, you'd likely get errors or weird behavior.

There are many questions on StackOverflow about this problem. Plugins shouldn't assume much about the page they're loaded in, so they use this pattern defensively.

like image 130
Brigand Avatar answered Sep 21 '22 15:09

Brigand