有人可以用Javascript解释美元符号吗?

本文翻译自:Can someone explain the dollar sign in Javascript?

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? 变量名中美元符号的目的是什么,为什么不排除呢?


#1楼

参考:https://stackoom.com/question/3YEb/有人可以用Javascript解释美元符号吗


#2楼

Here is a good short video explanation: https://www.youtube.com/watch?v=Acm-MD_6934 这是一个很好的简短视频说明: 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. 根据Ecma国际标识符名称是根据Unicode标准第5章 “标识符”部分中给出的语法解释的标记,并进行了一些小的修改。 An Identifier is an IdentifierName that is not a ReservedWord ( see 7.6.1 ). 标识符是不是ReservedWord的IdentifierName( 见7.6.1 )。 The Unicode identifier grammar is based on both normative and informative character categories specified by the Unicode Standard. Unicode标识符语法基于Unicode标准指定的规范和信息字符类别。 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: 必须将所有符合ECMAScript实现的Unicode标准3.0版中指定类别中的字符视为这些类别。此标准指定特定字符添加:

The dollar sign ($) and the underscore (_) are permitted anywhere in an IdentifierName . 在IdentifierName中的任何位置都允许使用美元符号($)和下划线(_)

Further reading can be found on: http://www.ecma-international.org/ecma-262/5.1/#sec-7.6 进一步阅读可在以下网址找到: 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). Ecma International 是一家成立于1961年的行业协会,致力于信息通信技术(ICT)和消费电子(CE)的标准化。


#3楼

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 但是 ,在ECMAScript 6(ES6)中,$可能代表模板文字

var user = 'Bob'
console.log(`We love ${user}.`); //Note backticks
// We love Bob.

#4楼

Dollar sign is used in ecmascript 2015-2016 as 'template literals'. 美元符号在ecmascript 2015-2016中用作'模板文字'。 Example: 例:

var a = 5;
var b = 10;
console.log(`Sum is equal: ${a + b}`); // 'Sum is equlat: 15'

Here working example: https://es6console.com/j3lg8xeo/ Notice this sign " ` ",its not normal quotes. 这里的工作示例: https//es6console.com/j3lg8xeo/注意这个符号“ ` ”,它不是正常的引号。

U can also meet $ while working with library jQuery . 在使用库jQuery时,U也可以满足$。

$ sign in Regular Expressions means end of line. $登录正则表达式表示行尾。


#5楼

When using jQuery, the usage of $ symbol as a prefix in the variable name is merely by convention; 使用jQuery时, $ symbol作为变量名中的前缀的用法仅仅是按惯例; it is completely optional and serves only to indicate that the variable holds a jQuery object, as in your example. 它是完全可选的,仅用于指示变量包含jQuery对象,如示例所示。

This means that when another jQuery function needs to be called on the object, you wouldn't need to wrap it in $() again. 这意味着当需要在对象上调用另一个jQuery函数时,您不需要再次将其包装在$() For instance, compare these: 例如,比较这些:

// the usual way
var item = $(this).parent().parent().find('input');
$(item).hide(); // this is a double wrap, but required for code readability
item.hide(); // this works but is very unclear how a jQuery function is getting called on this 

// with $ prefix
var $item = $(this).parent().parent().find('input');
$item.hide(); // direct call is clear
$($item).hide(); // this works too, but isn't necessary

With the $ prefix the variables already holding jQuery objects are instantly recognizable and the code more readable, and eliminates double/multiple wrapping with $() . 使用$前缀,已经拥有jQuery对象的变量可以立即识别,代码更具可读性,并消除了使用$()双/多包装。


#6楼

No reason. 没理由。 Maybe the person who coded it came from PHP. 也许编码它的人来自PHP。 It has the same effect as if you had named it "_item" or "item" or "item$$". 它具有与命名为“_item”或“item”或“item $$”相同的效果。

Maybe it's some kind of Hungarian notation for the coder to note that the variable is a DOM element or something. 也许这是编码器的某种匈牙利符号,注意变量是DOM元素或其他东西。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值