tags:

views:

1050

answers:

10

I have something simple like this:

$(selector).append("somestuff");

But since I'm going to reuse the selector I cache it with:

var $selector = $(selector);

So I end up with:

$selector.append("somestuff");

My question is, should I be doing that, or should I be doing:

var selector = $(selector);
selector.append("somestuff");

Trying either one, both works. Which method is correct, why. Is the '$' $selector unnecessary because the jquery object has already been declared in $(selector).

Thanks.

Edit:

Thanks for the answers, it seems very simple and quite clear. Still, there seems to be disagreement over whether or not I should use $ in the variable. It would be nice for everyone to vote up an answer. :)

+9  A: 

$ is just a name - names in JavaScript can contain dollars, and can consist of just a dollar.

Whether you use a dollar sign in your name isn't relevant to jQuery - there's nothing special about the dollar sign, except that jQuery defines a function called $.

RichieHindle
+2  A: 

I've seen it done both ways. All you are doing is creating a variable with the name '$selector', so they are functionally equivalent. The nice thing about it is that it does make them easy to pick out as jQuery objects.

Michael Morton
+4  A: 

The cash sign is just an alias for the jQuery function. Starting the variable name with $ has no affect on that variable.

It is customary though, especially in jQuery plugin development, to use variables already wrapped in jQuery with a cash sign, so you know you can call jQuery methods, without having to wrap them.

Ben
+1. $ prefix to your variable indicates that it's a jQuery object and you can call all the jQuery methods without thinking whether you need to pass the variable to the jQuery method.
SolutionYogi
+2  A: 

Yeah.

jQuery always returns a jQuery object. It's what allows chaining.

You've already defined the variable so you get a jQuery object back so $ is unnecessary

easement
A: 

It doesn't really matter, it's only naming.

I would go with...

var selector = $(selector);
selector.append("somestuff");

... because having variable names starting with a $ sign is a bit non conventional

marcgg
+1  A: 

i like to prefix all my jquery object names with $ so that i know its actually a jquery object, not a dom reference.

its a good naming convention

mkoryak
A: 

I don't believe that you need to add the jQuery selector to jQuery objects (since it's already a part of the object). We don't add the selector and haven't run into any problems.

Grant Heaslip
A: 

"$" is a function in jQuery. So when you call $(selector), you're actually calling the function $ with selector as the argument.

Generally, don't use the "$" as part of a variable name for javascript. You will only confuse yourself.

vh
+5  A: 

RichieHindle is correct. To expand:

Javascript variables allow the '$' character. So for example, you could have the following:

var $i = 1;
var i = 1;

Both i and $i have the same value, and both are perfectly legitimate variable names.

jQuery assigns itself (the jQuery object) to '$' because traditionally, that's what Javascript frameworks have done for selectors. There's no inherent meaning to '$' beyond what jQuery gives it.

thedz
A: 

With jQuery, the dollar sign symbol "$" is just a shortcut for "jQuery" therefore calling "$(...)" is the same thing as "jQuery(...)". Don't confuse a variable name that starts with the dollar sign with the JQuery shortcut - they have nothing to do with each other.

Matt Ball