为什么JavaScript变量以美元符号开头? [英] Why would a JavaScript variable start with a dollar sign?
问题描述
我经常看到带有以美元符号开头的变量的JavaScript。何时/为什么你会选择以这种方式为变量添加前缀?
I quite often see JavaScript with variables that start with a dollar sign. When/why would you choose to prefix a variable in this way?
(我不是在询问 $('p.foo')
您在jQuery和其他人看到的语法,但是正常的变量,如 $ name
和 $ order
)
(I'm not asking about $('p.foo')
syntax that you see in jQuery and others, but normal variables like $name
and $order
)
推荐答案
jQuery中一个非常常见的用法是将存储在变量中的jQuery对象与其他变量区分开来。例如,我会定义
A very common use in jQuery is to distinguish jQuery objects stored in variables from other variables. 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
我觉得这对编写jQuery代码很有帮助很容易看到具有不同属性集的jQuery对象。
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.
这篇关于为什么JavaScript变量以美元符号开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!