When/why to prefix variables with "$" when using jQuery? [duplicate]
Asked Answered
S

5

75

Possible Duplicate:
Why would a javascript variable start with a dollar sign?

I see people using the dollar sign in front of variables when using jQuery. Is there any reason behind this? I'm I missing something basic or is it just a common practice?

Shoa answered 2/6, 2011 at 1:25 Comment(2)
I actually didn't find anything useful. Missed that one! Thx.Shoa
This isn't a duplicate for that question -- this question focuses explicitly on usage in a jQuery context and has received a completely different set of responses.Worley
K
97

It's a common reference to a jQuery wrapped object. It makes reading the code easier to know which variables are jQuery wrapped.

//Item has been "cached" for later use in the script as a jQuery object.
var $item = $(this);
Kowalski answered 2/6, 2011 at 1:27 Comment(12)
Using the $ in front of variable names is what is called "hungarian notation" and is no different from doing int_count, arry_list or str_name. This is not wrong, but many people see this as an outdated way of naming variables.Jobye
In the context of using jQuery this is not outdated, but a regular practice.Kowalski
Regular doesn't exclude outdated: An excellent read on Hungarian notation and jQuery: bennadel.com/blog/…Jobye
An opinion -- not fact -- based blog post with a conclusion that basically states that it's a preference to use camel case over hungarian notation. A hybrid of both achieves maximum readability.Kowalski
I have never used -- or seen -- $item notation used in code I have worked on, even to indicate it contains a jQuery object. However, this usage might be arguably Apps Hungarian as the variable "represent a jQuery object" and not System Hungarian (which is normally considered "the bad kind"). The line is very blurred due to lack of static type information, of course.Worley
The fact that the guy had to go on stackoverflow to figure out what the $ prefix meant is an indicator that it is a bad practice. Code should be self-describingKatsuyama
@Katsuyama , an assignment statement is about as self describing as any code could be. X = Y ... no other way to say that. If folks didn't have to come here for reference regarding how to do something or what something meant, then there would be no point to have Stack Overflow, reference books, etc.; it would all be "obvious".Kowalski
I've seen this notation used in typescript too and don't see the reason for it, since we have intellisense and it's just an extra letter which makes it look ugly. Code should be beautiful and readable without having to prefix variable names. I'd be more concerned with using the variable name item instead of prefixing variables with $. If someone is looking over some code that is making use of JQuery, it should be clear enough that they are going to come across JQuery objects. Just my opinion :)Lim
I like the $ prefix convention and will continue using it. :)Neustria
personally this notation is an annoyance to the readers eye. currently I am reading a "code" that littered with $ prefix.Obsess
Hungarian notation is of little use when you have a type system — not in JavaScript. And jQuery code sometimes mixes jQuery-wrapper object and not-wrapped objects.Embolus
when you come back to jQuery after a long pause, you feel the need to have used $ for cached jQuery objects... learnt to use it now onwardsPoliard
H
44

For me a common practice is this:

If a variable is private I use an underscore like this:

(function(){
     var _foo = "bar";
})()

If it's public I'll use no underscore:

var foo = "bar"

And if it's a jQuery selector I'll use the $:

var $foo = $('bar');
//then you can access it like this
$foo.attr('id')

It's just coding convention and it allows you to quickly reference what type the variable is later in the code.

Helluva answered 2/6, 2011 at 1:31 Comment(1)
This seems like a good practice.Shoa
G
7

Many people using jQuery will prefix variables that contain a jQuery object with a $, so that they are easily identified. Consider this example:

var $img = $(".someclass span.otherclass img");
/* somewhere later in the code */
$img.bind("click", function() {/*...*/});
Grasping answered 2/6, 2011 at 1:30 Comment(0)
M
3

In my experience this is just a readability. Some developers like to prefix their variables so that they are easy to spot. It could also be a PHP habit creeping it's way in to Javascript.

Medick answered 2/6, 2011 at 1:28 Comment(0)
P
2

Dollar signs in code that uses JQuery commonly means that the variable in question is a jQuery variable (an object wrapped by jquery).

Propend answered 2/6, 2011 at 1:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.