Why do jQuery everywhere have dollar signs?

I am working on a project with quite a lot of jQuery. JQuery has many $ signs everywhere, for example

$(document).ready(function () { $('input[type=file]').wl_File({ url: '/Admin/PolicyInventory/UploadDocuments', onFileError: function (error, fileobj) { $.msg('file is not allowed: ' + fileobj.name, { header: error.msg + ' Error ', live: 10000 }); } }); ... 

My question is: what does this dollar sign mean? Why is it used everywhere and how to understand and interpret it? This reminds me of the terrible days when I studied the Scheme at the university, and I had to put brackets everywhere, not knowing why I was doing this.

+82
jquery
May 28 '12 at 15:53
source share
7 answers

$ is just a shortcut to jQuery . The idea is that everything runs with one global character (since the global namespaces are ridiculously crowded), jQuery , but you can use $ (because it's shorter) if you want:

 // These are the same barring your using noConflict (more below) var divs = $("div"); // Find all divs var divs = jQuery("div"); // Also find all divs, because console.log($ === jQuery); // "true" 

If you do not want to use an alias, you do not need it. And if you want $ not to be an alias for jQuery , you can use noConflict and the library will restore $ to what it was before jQuery took this. (Useful if you also use Prototype or MooTools.)

+133
May 28 '12 at 15:55
source share

It is just a convenient character, shorter to enter and easier to read than jQuery.

There is nothing special except that it has not traditionally been used to run a variable or function name, which reduces the risk or collision of names.

+10
May 28 '12 at 15:54
source share
Icon

$ is an alias for jQuery . A short version of jQuery , less writing mechanism.

For example only: (in jQuery this is more complicated)

 var yourFunction = function() { alert('a function'); } window.Myf = yourFunction; 

Now you can call yourFunction as:

 Myf(); // definitely a short syntax 
+7
May 28 '12 at 15:55
source share

Rigidity and performance




When we are working on a library or programming language, we should pay attention to some writing rules. Thanks to jQuery, they have already implemented many options. You can use $ , or you can use jQuery , or you can use _

 (function (_) { _("#wow").click() })(jQuery); 

Or maybe you can pretend javascript identifiers are unicode, so you can use Ω

 (function (Ω) { Ω("#wow").click() })(jQuery); 

But the main idea of ​​this, pressing the keyboard once is better than typing jQuery




On the other hand, we have performance ... I just accidentally opened my project and looked for $ , I used $ 54 in a single javascript file.

$ is a byte.

jQuery - 6 bytes.

The difference is huge 54 * 5 = 220 bytes.

+6
Feb 23 '16 at 16:22
source share

In javascript, $ (one dollar symbol) is a valid variable name. Several frameworks, including jQuery, have adopted it as a synonym for an object that contains convenient top-level methods provided by the infrastructure.

+4
May 28 '12 at 15:57
source share

Google is your friend: $ sign jQuery

The dollar sign is just an alias for jQuery.

 jQuery(document).ready(function(){}); 

OR

 $(document).ready(function(){}); 
+3
May 28 '12 at 15:55
source share
Sign

$ is used as an alias for jQuery. instead of using jquery.hide, jquery.show, where we can use the alias $ ($. hide) since we use this word many times. "JQuery" will not be a convenient way, so we will use the $ alias. If we want to change it, we can change it with the noConflict method var Sample = $. noConflict ()

0
Jun 25 '15 at 4:25
source share



All Articles