The origin of the $ symbol in jQuery
$ is a famous jQuery symbol. In fact, jQuery encapsulates all functions in a global variable jQuery, and $ is also a legal variable name. It is an alias of the variable jQuery:
window.jQuery ; // jQuery(selector, context)
- ##window.$; // jQuery(selector, context) ##$ === jQuery ; // true
- ##typeof($); // 'function'
$ is essentially a function, but the function It is also an object, so in addition to being directly called, $ can also have many other attributes.
Note: The $function name you see may not be jQuery(selector, context), because many JavaScript compression tools can rename function names and parameters, so the compressed jQuery source code $function may become a(b,c).
Most of the time, we use $ directly (because it is easier to write). However, if the $ variable is unfortunately occupied and cannot be changed, then we can only ask jQuery to hand over the $ variable, and then we can only use the jQuery variable:
$; // jQuery(selector, context)##jQuery.noConflict();
- ##$; // undefined
- jQuery; // jQuery(selector, context)
- The principle of this black magic is that jQuery saves the original internally before occupying $ $, when calling jQuery.noConflict(), the originally saved variables will be restored