javascript 符号_JavaScript中的美元符号($)和下划线(_)

javascript 符号

The dollar sign ($) and the underscore (_) characters are JavaScript identifiers, which just means that they identify an object in the same way a name would. The objects they identify include things such as variables, functions, properties, events, and objects.

美元符号( $ )   下划线( _ )字符是JavaScript 标识符 ,这仅表示它们以与名称相同的方式标识对象。 他们标识的对象包括诸如变量,函数,属性,事件和对象之类的东西。

For this reason, these characters are not treated the same way as other special symbols. Instead, JavaScript treats $ and _ as if they were letters of the alphabet.

因此,这些字符与其他特殊符号的处理方式不同。 相反,JavaScript将$_当作字母来对待。

A JavaScript identifier — again, just a name for any object — must start with a lower or upper case letter, underscore (_), or dollar sign ($); subsequent characters can also include digits (0-9). Anywhere that an alphabetic character is allowed in JavaScript, 54 possible letters are available: any lowercase letter (a through z), any uppercase letter (A through Z), $ and _.

同样,JavaScript标识符(仅是任何对象的名称)必须小写或大写字母,下划线( _ )或美元符号( $ )开头; 后续字符也可以包括数字(0-9)。 在JavaScript中允许使用字母字符的任何地方,都可以使用54个可能的字母:任何小写字母(a到z),任何大写字母(A到Z), $_

美元($)标识符 ( The Dollar ($) Identifier )

The dollar sign is commonly used as a shortcut to the function document.getElementById(). Because this function is fairly verbose and used frequently in JavaScript, the $ has long been used as its alias, and many of the libraries available for use with JavaScript create a $() function that references an element from the DOM if you pass it the id of that element.

美元符号通常用作函数document.getElementById()的快捷方式。 由于此函数相当冗长且经常在JavaScript使用 ,因此$长期以来一直用作其别名,并且许多可用于JavaScript的库都创建了一个$()函数,如果您将其传递给DOM,则该函数会引用DOM中的元素。该元素的ID。

There is nothing about $ that requires it to be used this way, however. But it has been the convention, although there is nothing in the language to enforce it.

但是,没有任何关于$的要求。 但这是约定俗成的,尽管没有什么语言可以强制执行。

The dollar sign $ was chosen for the function name by the first of these libraries because it is a short one-character word, and $ was least likely to be used by itself as a function name and therefore the least likely to clash with other code in the page.

在这些库中的第一个中,函数名选择了美元符号$ ,因为它是一个短的单字符单词,并且$本身最不可能用作函数名,因此与其他代码冲突的可能性最小。在页面中。

Now multiple libraries are providing their own version of the $() function, so many now provide the option to turn off that definition in order to avoid clashes. 

现在,多个库都提供了自己的$()函数版本,因此许多库现在提供了关闭该定义的选项,以避免发生冲突。

Of course, you don't need to use a library to be able to use $(). All you need to substitute $() for document.getElementById() is to add a definition of the $() function to your code as follows:

当然,您无需使用库即可使用$() 。 将$()替换为document.getElementById ()所需要做的就是向代码中添加$()函数的定义,如下所示:


function $(x) {return document.getElementById(x);} 

下划线_标识符 ( The Underscore _ Identifier  )

A convention has also developed regarding the use of _, which is frequently used to preface the name of an object's property or method that is private. This is a quick and easy way to immediately identify a private class member, and it is so widely used, that almost every programmer will recognize it.

关于_的使用,也已经开发了一个约定,该约定通常用于在对象的私有属性或方法的名称前添加前缀。 这是一种立即识别私有类成员的快速简便的方法,它的用途非常广泛,几乎每个程序员都可以识别它。

This is particularly useful in JavaScript since defining fields as private or public is done without the use of the private and public keywords (at least this is true in the versions of JavaScript used in web browsers — JavaScript 2.0 does allow these keywords).

这在JavaScript中特别有用,因为无需使用privatepublic关键字就可以将字段定义为private或public(至少在Web浏览器中使用JavaScript版本中是这样-JavaScript 2.0允许使用这些关键字)。

Note that again, as with $, the use of _ is merely a convention and is not enforced by JavaScript itself. As far as JavaScript is concerned, $ and _ are just ordinary letters of the alphabet.

再次注意,与$一样_的使用仅是约定,并非由JavaScript本身强制执行。 就JavaScript而言, $_只是字母表中的普通字母。

Of course, this special treatment of $ and _ applies only within JavaScript itself. When you test for alphabetic characters in the data, they are treated as special characters no different from any of the other special characters.

当然,对$_的特殊处理仅适用于JavaScript本身。 在数据中测试字母字符时,它们被视为特殊字符,与其他任何特殊字符没有区别。

翻译自: https://www.thoughtco.com/and-in-javascript-2037515

javascript 符号

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值