$ versus jQuery

We are switching from MooTools to jQuery at work. I have an employee who told me to never use $ as a prefix when using the jQuery method (if I understood it correctly). He said that instead, a more appropriate or safer way (I really didn't follow it) was to use jQuery. .

Therefore, instead of $.plugin() , you should use jQuery.plugin() , for example.

Why so? What difference does it make with $ / jQuery ? Should I forget about the dollar sign as my accessory? Only in certain circumstances?

+59
jquery
Feb 06 '10 at 9:01
source share
4 answers

Why so? Is this the difference in $ / jQuery right?

Because almost every JavaScript library defines a function called $. When you have many libraries in one document, conflicts can arise. If you are sure that jQuery is and always will be the only script defining this function, I would have nothing against using $.

jQuery defines a good solution to conflict resolution: jQuery.noConflict . Using this function, you can determine your name, as a result of which jQuery will be available, for example

 var $jq = jQuery.noConflict(true); 

Calling this function will restore previous values ​​for the $ and jQuery variables when they existed before jQuery initialization. I don’t remember if any other libraries tried to resolve name conflicts.

If you want to use $ instead of jQuery, you can run your code all the time in a separate private area that contains the definition of $ using the self-launch function.

 (function($){ // your code goes here $("body").append("<p>Hello World!</p>"); })(jQuery); // or even jQuery.noConflict() 
+73
Feb 06 2018-10-06T00 06
source share

$ is an alias of jQuery and could theoretically be an alias for something else in another included library or script, which can lead to confusion or worse. If you are using jquery only, using $ should be great.

+10
Feb 06 '10 at 9:07
source share

To avoid conflict with other libraries, such as Prototype, which use the same $.

I prefer $, and there is no problem using it if you only use jQuery.

+3
Feb 06 '10 at 9:29
source share

In addition to other responses to conflicts with other libraries, the variable name must always describe the variable. jQuery is a much more descriptive variable name than $ .

+3
May 13 '15 at
source share



All Articles