Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

javascript - 为什么JavaScript变量会以美元符号开头?(Why would a JavaScript variable start with a dollar sign?)

I quite often see JavaScript with variables that start with a dollar sign.

(我经常看到JavaScript带有以美元符号开头的变量。)

When/why would you choose to prefix a variable in this way?

(您何时/为什么选择以这种方式为变量添加前缀?)

(I'm not asking about $('p.foo') syntax that you see in jQuery and others, but normal variables like $name and $order )

((我不是在问您在jQuery和其他语言中看到的$('p.foo')语法,而是普通变量,例如$name$order ))

  ask by Ken translate from so

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

1 Answer

0 votes
by (71.8m points)

A very common use in jQuery is to distinguish jQuery objects stored in variables from other variables.

(jQuery中非常普遍的用法是将存储在变量中的jQuery对象与其他变量区分开。)

For example, I would define

(例如,我将定义)

var $email = $("#email"); // refers to the jQuery object representation of the dom object
var email_field = $("#email").get(0); // refers to the dom object itself

I find this to be very helpful in writing jQuery code and makes it easy to see jQuery objects which have a different set of properties.

(我发现这对于编写jQuery代码非常有帮助,并且可以轻松查看具有不同属性集的jQuery对象。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...