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, and why? Is the $
in $selector
unnecessary because the jquery object has already been declared in $(selector)
?
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. :)
Symbol for the American dollar in English Write the country symbol ( US ) first, immediately followed by the dollar sign ($) and the dollar figure: US$ 25.99.
You may have heard that the dollar sign started as a U on top of an S, as in “United States.” Over time, the bottom of the U disappeared, leaving the S with two lines through it, which was eventually simplified to only one line.
It stands for the initials of the United States'.
Buck is an informal reference to $1 that may trace its origins to the American colonial period when deerskins (buckskins) were commonly traded for goods. The buck also refers to the U.S. dollar as a currency that can be used both domestically and internationally.
$
is just a name - names in JavaScript can contain dollar signs, and can consist of just a dollar sign.
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 $
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With