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)
?
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. :)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…