JavaScript 中美元符号的用途是什么?
有问题的代码在这里:
var $item = $(this).parent().parent().find('input');
变量名称中美元符号的用途是什么,为什么不直接排除它?
The code in question is here:
var $item = $(this).parent().parent().find('input');
What is the purpose of the dollar sign in the variable name, why not just exclude it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
变量中的“$”对于解释器来说没有什么特殊意义,就像下划线一样。
据我所知,许多使用 jQuery 的人(这就是你的示例代码对我来说的样子)倾向于为变量添加前缀包含一个带有 $ 的 jQuery 对象,这样它们很容易被识别,并且不会与整数等混淆。
jQuery 中的美元符号函数
$()
是一个经常使用的库函数,因此需要一个简短的名称。A '$' in a variable means nothing special to the interpreter, much like an underscore.
From what I've seen, many people using jQuery (which is what your example code looks like to me) tend to prefix variables that contain a jQuery object with a $ so that they are easily identified and not mixed up with, say, integers.
The dollar sign function
$()
in jQuery is a library function that is frequently used, so a short name is desirable.在您的示例中,$ 除了作为名称的字符之外没有特殊意义。
但是,在 ECMAScript 6 (ES6) 中,$ 可能代表 模板文字
In your example the $ has no special significance other than being a character of the name.
However, in ECMAScript 6 (ES6) the $ may represent a Template Literal
美元符号的处理方式与普通字母或下划线 (
_
) 相同。 它对口译员来说没有特殊意义。与许多类似的语言不同,Javascript 中的标识符(例如函数名和变量名)不仅可以包含字母、数字和下划线,还可以包含美元符号。 它们甚至可以以美元符号开头,或者仅包含一个美元符号而没有其他内容。
因此,
$
是 Javascript 中有效的函数或变量名称。为什么要在标识符中使用美元符号?
语法并不真正强制标识符中美元符号的任何特定用法,因此这取决于您希望如何使用它。 过去,经常建议仅在生成的代码中使用美元符号开头的标识符 - 也就是说,代码不是手动创建的,而是由代码生成器创建的。
然而,在您的示例中,情况似乎并非如此。 看起来有人只是为了好玩而在开头加了一个美元符号 - 也许他们是一位 PHP 程序员,出于习惯或其他原因这样做。 在 PHP 中,所有变量名称前面都必须有一个美元符号。
如今,美元符号在解释器中还有另一个常见含义:jQuery 对象,其名称仅由单个美元符号 (
$
) 组成。 这是从早期的 Javascript 框架(如 Prototype)借用的约定,如果 jQuery 与其他此类框架一起使用,将会出现名称冲突,因为它们都使用名称$
(jQuery 可以配置为使用其全局对象的不同名称)。 Javascript 中没有什么特别之处,允许 jQuery 使用单个美元符号作为其对象名称; 如上所述,它只是另一个有效的标识符名称。The dollar sign is treated just like a normal letter or underscore (
_
). It has no special significance to the interpreter.Unlike many similar languages, identifiers (such as functional and variable names) in Javascript can contain not only letters, numbers and underscores, but can also contain dollar signs. They are even allowed to start with a dollar sign, or consist only of a dollar sign and nothing else.
Thus,
$
is a valid function or variable name in Javascript.Why would you want a dollar sign in an identifier?
The syntax doesn't really enforce any particular usage of the dollar sign in an identifier, so it's up to you how you wish to use it. In the past, it has often been recommended to start an identifier with a dollar sign only in generated code - that is, code created not by hand but by a code generator.
In your example, however, this doesn't appear to be the case. It looks like someone just put a dollar sign at the start for fun - perhaps they were a PHP programmer who did it out of habit, or something. In PHP, all variable names must have a dollar sign in front of them.
There is another common meaning for a dollar sign in an interpreter nowadays: the jQuery object, whose name only consists of a single dollar sign (
$
). This is a convention borrowed from earlier Javascript frameworks like Prototype, and if jQuery is used with other such frameworks, there will be a name clash because they will both use the name$
(jQuery can be configured to use a different name for its global object). There is nothing special in Javascript that allows jQuery to use the single dollar sign as its object name; as mentioned above, it's simply just another valid identifier name.$ 符号是变量和函数的标识符。
https://web.archive.org/web /20160529121559/http://www.authenticsociety.com/blog/javascript_dollarsign
这对美元符号的用途有明确的解释。
这是另一种解释:http://www.vcarrer。 com/2010/10/about-dollar-sign-in-javascript.html
The $ sign is an identifier for variables and functions.
https://web.archive.org/web/20160529121559/http://www.authenticsociety.com/blog/javascript_dollarsign
That has a clear explanation of what the dollar sign is for.
Here's an alternative explanation: http://www.vcarrer.com/2010/10/about-dollar-sign-in-javascript.html
美元符号在 ecmascript 2015-2016 中用作“模板文字”。
示例:
这里是工作示例:
https://es6console.com/j3lg8xeo/
注意这个符号“`”,它不是正常的引号。
您还可以在使用 jQuery 库时遇到 $。
正则表达式中的$符号表示行结束。
Dollar sign is used in ecmascript 2015-2016 as 'template literals'.
Example:
Here working example:
https://es6console.com/j3lg8xeo/
Notice this sign " ` ",its not normal quotes.
U can also meet $ while working with library jQuery.
$ sign in Regular Expressions means end of line.
我将添加以下内容:
在 chromium 浏览器的开发人员控制台(尚未尝试其他)中,
$
是一个本机函数,其作用类似于document.querySelector
,很可能是受别名启发来自 JQuery 的$
I'll add this:
In chromium browser's developer console (haven't tried others) the
$
is a native function that acts just likedocument.querySelector
most likely an alias inspired from JQuery's$
使用 jQuery 时,使用
$
符号作为变量名的前缀只是按照惯例; 它是完全可选的,仅用于指示该变量包含 jQuery 对象,如您的示例中所示。这意味着当需要在该对象上调用另一个 jQuery 函数时,您不需要再次将其包装在
$()
中。 例如,比较一下这些:使用
$
前缀,已经保存 jQuery 对象的变量可以立即识别,并且代码更具可读性,并且消除了$()
的双重/多重包装。When using jQuery, the usage of
$
symbol as a prefix in the variable name is merely by convention; it is completely optional and serves only to indicate that the variable holds a jQuery object, as in your example.This means that when another jQuery function needs to be called on the object, you wouldn't need to wrap it in
$()
again. For instance, compare these:With the
$
prefix the variables already holding jQuery objects are instantly recognizable and the code more readable, and eliminates double/multiple wrapping with$()
.没有理由。 也许编写它的人来自 PHP。 它具有与将其命名为“_item”或“item”或“item$$”相同的效果。
作为后缀(如“item$”,发音为“items”),它可以表示可观察的对象,例如 DOM 元素,作为称为 "芬兰表示法" 类似于匈牙利表示法。
No reason. Maybe the person who coded it came from PHP. It has the same effect as if you had named it "_item" or "item" or "item$$".
As a suffix (like "item$", pronounced "items"), it can signify an observable such as a DOM element as a convention called "Finnish Notation" similar to the Hungarian Notation.
这是一个很好的简短视频说明:https://www.youtube.com/watch?v =Acm-MD_6934
根据 Ecma 国际标识符名称是根据 Unicode 标准第 5 章,进行了一些小的修改。 标识符是不是保留字的标识符名称(参见 7.6。 1)。 Unicode 标识符语法基于 Unicode 标准指定的规范性和信息性字符类别。 所有符合 ECMAScript 实现的 Unicode 标准 3.0 版中指定类别中的字符必须被视为这些类别中的字符。此标准指定了特定的字符添加:
允许使用美元符号 ($) 和下划线 (_) IdentifierName 中的任何位置。
进一步阅读可在以下位置找到:http://www.ecma-international .org/ecma-262/5.1/#sec-7.6
Ecma International是一个行业协会,成立于 1961 年,致力于信息标准化以及通信技术 (ICT) 和消费电子产品 (CE)。
Here is a good short video explanation: https://www.youtube.com/watch?v=Acm-MD_6934
According to Ecma International Identifier Names are tokens that are interpreted according to the grammar given in the “Identifiers” section of chapter 5 of the Unicode standard, with some small modifications. An Identifier is an IdentifierName that is not a ReservedWord (see 7.6.1). The Unicode identifier grammar is based on both normative and informative character categories specified by the Unicode Standard. The characters in the specified categories in version 3.0 of the Unicode standard must be treated as in those categories by all conforming ECMAScript implementations.this standard specifies specific character additions:
The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName.
Further reading can be found on: http://www.ecma-international.org/ecma-262/5.1/#sec-7.6
Ecma International is an industry association founded in 1961 and dedicated to the standardization of Information and Communication Technology (ICT) and Consumer Electronics (CE).
通过 https://www.w3schools.com/js/js_variables.asp< /a>
via https://www.w3schools.com/js/js_variables.asp
我可能会补充一点,将它用于 jQuery 可以让你做这样的事情,例如:
I might add that using it for jQuery allows you to do things like this, for instance:
这会显示“Hello World!” 警报框。
正如您所看到的,就 JavaScript 标识符或变量名称而言,
$
只是一个普通字符。 事实上,您可以使用大量 Unicode 字符作为看起来像美元或其他货币符号的变量名称!请小心,因为
$
符号还用作对 jQuery 命名空间/库的引用:$
也用于新的 jQuery 中。 JavaScript 版本 ES6/2015 支持使用字符串插值的 >Template Literal 格式:This displays a "Hello World!" alert box.
As you can see,
$
is just a normal character as far as JavaScript identifiers or variable names go. In fact you can use a huge range of Unicode characters as variable names that look like dollar or other currency signs!Just be careful as the
$
sign is also used as a reference to the jQuery namespace/library:$
is also used in the new Template Literal format using string interpolation supported in JavaScript version ES6/2015: