Reputation: 46750
I am working on a project with quite a lot of JQuery in it. The JQuery has a lot of $ 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 all over the place and how do I understand and interpret it? It reminds me of the scary days of when I was learning Scheme at University and had to put brackets everywhere without knowing why I was doing it.
Upvotes: 127
Views: 96749
Reputation: 1074395
$
is just a shortcut for jQuery
. The idea is that everything is done with the one global symbol (since the global namespaces is ridiculously crowded), jQuery
, but you can use $
(because it's shorter) if you like:
// These are the same (unless you use noConflict, more below)
const divs1 = $("div"); // Find all divs
const divs2 = jQuery("div"); // Also find all divs, because $ === jQuery
console.log($ === jQuery); // true
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
If you don't want to use the alias, you don't have to. And if you don't want $
to be an alias for jQuery
, you can use noConflict
and the library will restore $
to whatever it was before jQuery took it over. (Useful if you also use Prototype or MooTools.)
Upvotes: 182
Reputation: 5462
When we are working on library or a programming language we should pay attention to some writability rules. Thanks to jQuery they already implemented lots of options. You can use $
or you can use jQuery
or you can use _
(function (_) {
_("#wow").click()
})(jQuery);
Or maybe you can do fancy changes, javascript identifiers are unicode so you can use Ω
(function (Ω) {
Ω("#wow").click()
})(jQuery);
But the main idea behind it, pressing once to the keyboard is better than typing jQuery
On the other side, we have performance... I just randomly opened one of my projects and searched for $
, I used 54 $
in a single javascript file.
$
is a byte.
jQuery
is 6 bytes.
The difference is huge 54 * 5 = 220 bytes.
Upvotes: 12
Reputation: 87073
$
sign is an alias for jQuery
. A short version of jQuery
, a less write mechanism.
Just for an example: (in jQuery it's more complicated)
var yourFunction = function() {
alert('a function');
}
window.Myf = yourFunction;
Now you can call yourFunction
like:
Myf(); // definitely a short syntax
Upvotes: 13
Reputation: 6778
$ sign is used as an alias to Jquery. instead of using jquery.hide,jquery.show every where we can use the alias $ ($.hide) Since we are using this word lot of times. 'Jquery' will not be a convenient way so we are using the alias $. If we want to change it we can change it by noConflict method var Sample=$.noConflict()
Upvotes: 2
Reputation: 382150
It's just a convenient character, shorter to type and easier to read than "jQuery".
There is nothing special except that it's traditionally not used to start a variable or function name, which reduces the risk or name collision.
Upvotes: 13
Reputation: 16841
In javascript, $
(a single dollar character) is a valid variable name. Several frameworks, among which jQuery, have adopted it as a synonym of an object that contain the top-level convenience methods the framework provides.
Upvotes: 4
Reputation: 14430
Google is your friend: $ sign JQuery
Dollar Sign is just an alias for JQuery.
jQuery(document).ready(function(){});
OR
$(document).ready(function(){});
Upvotes: 7