Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
170 views
in Technique[技术] by (71.8m points)

jquery - The $ dollar sign

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. :)

question from:https://stackoverflow.com/questions/1180213/the-dollar-sign

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

$ 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 $.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...