为什么在 javascript 变量的名称中使用 $(美元符号)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3360858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 04:18:39  来源:igfitidea点击:

Why use $ (dollar sign) in the name of javascript variables?

javascriptjquery

提问by Simon

Possible Duplicates:
Why would a javascript variable start with a dollar sign?
JQuery : What is the difference between “var test” and “var $test”

可能的重复:
为什么 javascript 变量以美元符号开头?
JQuery:“var test”和“var $test”有什么区别

What is the difference between this two ways of initializing variables?

这两种初始化变量的方式有什么区别?

var $val = 'something'

     OR

var val = 'something'

as I see they are the same thing.

正如我所见,它们是同一回事。

Maybe in this case $is only the part of name in variable? (it will become a meaningless question in that case:/)

也许在这种情况下$只是变量中的名称部分?(在这种情况下,这将成为一个毫无意义的问题:/)

Thanks

谢谢

回答by Konerak

The $in the variable name is only part of the name, but the conventionis to use it to start variable names when the variable represents a jQuery object.

$变量名是名字中的一部分,但惯例是用它当变量代表了一个jQuery对象开始的变量名。

var $myHeaderDiv = $('#header');
var myHeaderDiv = document.getElementById('header');

Now later in your code, you know the $myHeaderDivis already a jQuery object, so you can call jQuery functions:

现在在您的代码中,您知道$myHeaderDiv已经是一个 jQuery 对象,因此您可以调用 jQuery 函数:

$myHeaderDiv.fade();

To get from the DOM-variable to the jQuery variable:

从 DOM 变量到 jQuery 变量:

var $myHeaderDiv = jQuery(myHeaderDiv); //assign to another variable
jQuery(myHeaderDiv).fade(); //use directly

//or, as the $ is aliased to the jQuery object if you don't specify otherwise:
var $myHeaderDiv = jQuery(myHeaderDiv); //assign
$(myHeaderDiv).fade(); //use

To get from the jQuery variable to the DOM-variable.

从 jQuery 变量到 DOM 变量。

var myHeaderDiv = $myHeaderDiv.get(0);

回答by the_drow

You are correct. $ is a part of the name of the variable.
This is not perl or PHP :)

你是对的。$ 是变量名称的一部分。
这不是 perl 或 PHP :)

回答by Māris Kise?ovs

There are 28 letters in the alphabet as far as JavaScript is concerned. a-z, _ and $. Anywhere you can use a letter in JavaScript you can use $ as that letter. (<c> Fellgall @ http://www.webdeveloper.com/forum/showthread.php?t=186546)

就 JavaScript 而言,字母表中有 28 个字母。az、_ 和 $。在 JavaScript 中可以使用字母的任何地方都可以使用 $ 作为该字母。( <c> Fellgall @ http://www.webdeveloper.com/forum/showthread.php?t=186546)

In your example $val and val will be two different variable names.

在您的示例中, $val 和 val 将是两个不同的变量名称。

回答by Gabriele Petrioli

No real difference..

没有本质区别。。

It is usally used to signify a variable holding a jquery or other javascript framework object, because they can have shorthand $function..

它通常用于表示包含 jquery 或其他 javascript 框架对象的变量,因为它们可以具有速记$功能..

It is just easier to identify the type of the contents..

更容易识别内容的类型。

回答by jim tollan

syom - in my case, i use the $ prefix to indicate that it's a variable that is referenced by jquery. It's purely a part of the variable and not a reserved character.

syom - 在我的例子中,我使用 $ 前缀来表示它是一个被 jquery 引用的变量。它纯粹是变量的一部分,而不是保留字符。

keeps it easy to identify in long code runs..

在长代码运行中易于识别..

jim

吉姆