Javascript 声明变量 jQuery 和 JS
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6045665/
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
Declaring variables jQuery and JS
提问by eski
It may be a silly question but I haven't been able to find any documentations on this on the internet.
这可能是一个愚蠢的问题,但我无法在互联网上找到任何有关此的文档。
When declaring variables to use within Javascript i normally use var x = 0
but I've seen in jQuery tutorials that they use $x = 0
. What is the difference of those two ?
在声明要在 Javascript 中使用的变量时,我通常使用,var x = 0
但我在 jQuery 教程中看到他们使用$x = 0
. 这两者有什么区别?
Also, would i call those two variables the same way or do i need to use the $ mark before ?
So for example : for(i=0; i < x; i++)
or for(i=0; i < $x; i++)
另外,我会以相同的方式调用这两个变量还是需要在之前使用 $ 标记?例如:for(i=0; i < x; i++)
或for(i=0; i < $x; i++)
回答by T.J. Crowder
Your var x = 0;
is fine. The only reason people sometimes use a $
with jQuery-specific variables is that it helps them remember that the variable contains the result of a call to the $()
function in jQuery. The $
is actually part of the variable name, so you would use $
everywhere you referred to the variable.
你var x = 0;
的很好。人们有时将 a$
与 jQuery 特定变量一起使用的唯一原因是它帮助他们记住变量包含对$()
jQuery 中函数的调用结果。该$
实际上是变量名称的一部分,因此会使用$
无处不在,你提到的变量。
So this:
所以这:
var x = $(".foo"); // Find all elements with class "foo"
x.css("color", "blue"); // Make them all blue
Is exactly the same as this:
与此完全相同:
var $x = $(".foo"); // Find all elements with class "foo"
$x.css("color", "blue"); // Make them all blue
They've just put a $
at the beginning of the name.
他们只是$
在名字的开头加上了一个。
Note: You've quoted your "jQuery" example as simply $x = 0;
(without var
). Be sure you always declare your variables (using var
); otherwise, you're falling prey to the Horror of Implicit Globals.
注意:您已经简单地引用了您的“jQuery”示例$x = 0;
(没有var
)。确保你总是声明你的变量(使用var
);否则,您将成为隐式全局变量的牺牲品。
回答by Matt Ball
The dollar sign $
is not a special character in JavaScript. It is a legal character in variable naming. Here's what constitutes a legal identifier in JavaScript:
美元符号$
不是 JavaScript 中的特殊字符。它是变量命名中的合法字符。以下是JavaScript 中合法标识符的构成:
A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).
Starting with JavaScript 1.5, you can use ISO 8859-1 or Unicode letters such as ? and ü in identifiers. You can also use the \uXXXX Unicode escape sequences as characters in identifiers.
Some examples of legal names are
Number_hits
,temp99
, and_name
.
JavaScript 标识符必须以字母、下划线 (_) 或美元符号 ($) 开头;后续字符也可以是数字 (0-9)。由于 JavaScript 区分大小写,因此字母包括字符“A”到“Z”(大写)和字符“a”到“z”(小写)。
从 JavaScript 1.5 开始,您可以使用 ISO 8859-1 或 Unicode 字母,例如 ? 和 ü 在标识符中。您还可以使用 \uXXXX Unicode 转义序列作为标识符中的字符。
法律名称的一些例子是
Number_hits
,temp99
和_name
。
Some jQuery programmers (myself included) use the $
as a prefix on variable names to indicate that those variables refer to jQuery objects.
一些 jQuery 程序员(包括我自己)将$
用作变量名的前缀来指示这些变量引用jQuery 对象。
See also
也可以看看
回答by user113716
No difference. The $
is just another character in JavaScript.
没有不同。该$
是在JavaScript只是另一个角色。
It just happens that jQuery users use it to signify a variable that is referencing a jQuery object. There's no requirement though.
碰巧 jQuery 用户使用它来表示引用 jQuery 对象的变量。不过没有要求。