-2

I'm looking to understand exactly what the symbol is bound to and how it was bound, and what its underlying structure looks like.

I'm guessing it's done with a label but I'm not experienced enough with JavaScript to be sure.

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220
  • It's just a reference to the `jQuery` function object, nothing more.. – Mike Christensen Jan 22 '13 at 23:18
  • 1
    What have you done so far to learn javascript and jquery? – Saju Jan 22 '13 at 23:18
  • 1
    `var $ = jQuery` would suffice. You can read the source and find out for yourself! https://github.com/jquery/jquery/blob/master/src/core.js – Matt Ball Jan 22 '13 at 23:19
  • Have a look at it in a javascript console and you can see what it contains. you could also look at the non-minified source if you really want to see how it works in detail. – Dave Jan 22 '13 at 23:21
  • Yup. This isn't the first time it's been commented on. I'm just an ignorant 'merican... http://stackoverflow.com/q/9168261#comment11532595_9168261 http://stackoverflow.com/q/9731760#comment12375304_9731760 – Matt Ball Jan 22 '13 at 23:40
  • Hey @Aerovistae don't forget to mark an answer as correct if it helped you. – Jezen Thomas Jan 25 '13 at 08:57

3 Answers3

5

Odd question. The $ symbol is just an alias for jQuery.

So, instead of this:

jQuery('a').css('color','#f00');

...You can do this:

$('a').css('color','#f00');

It doesn't necessarily need to be $ either. If you like, you can ADD MORE MONEY. This works:

(function ($$$$$$) {
    $$$$$$('body').html('Nobody will mess with you when you have this much cash.');
}(jQuery));

It seems as though normal JavaScript variable naming rules apply:

  • Variable names must begin with a letter
  • Variable names can also begin with $ and _
  • Variable names are case sensitive

If you wanted, your alias could be orange_peanut, but let's not do that for obvious reasons.

Hope that helps! :)

Jezen Thomas
  • 13,619
  • 6
  • 53
  • 91
2

I remember being initially confused by this myself. But the $ is just a variable referring to the static, global JQuery object. There's absolutely nothing special about it being a dollar sign - it was just a convenient and short variable name that John Resig (presumably) assumed wasn't widely used elsewhere. You can see a similar pattern with the Underscore library using an underscore (_) as its default global variable.

Now, it does have some kind of odd calling patterns, because instead of a normal variable, where you use it like this:

myObj.myFunction(myParam); 

The JQuery static object is both an object and a function. So you can call it like this (a function):

$('div').empty();

Or sometimes like this (like an object):

$.ajax({url: "http://fiddle.jshell.net/favicon.png"});

But there are lots and lots and lots of things that it does, and lots of subtleties about how it does them. To get better information, just check out the JQuery documentation: http://api.jquery.com/. And look around at the JQuery questions here on SO - they've got lots of information. You'll pick it up eventually.

Ken Smith
  • 20,305
  • 15
  • 100
  • 147
0

The $ is an alias for the main jQuery() function. In other words, it is a function object, as are all functions in Javascript, and it has a number of properties on it.

GregL
  • 37,147
  • 8
  • 62
  • 67