有问题的代码在这里:
var $item = $(this).parent().parent().find('input');
在变量名中使用美元符号的目的是什么,为什么不直接排除它呢?
有问题的代码在这里:
var $item = $(this).parent().parent().find('input');
在变量名中使用美元符号的目的是什么,为什么不直接排除它呢?
当前回答
没有理由。也许编写代码的人来自PHP。它的效果与将其命名为“_item”或“item”或“item$$”相同。
作为一个后缀(如“item$”,发音为“items”),它可以表示一个可观察对象,如DOM元素,作为一种称为“芬兰符号”的约定,类似于匈牙利符号。
其他回答
没有理由。也许编写代码的人来自PHP。它的效果与将其命名为“_item”或“item”或“item$$”相同。
作为一个后缀(如“item$”,发音为“items”),它可以表示一个可观察对象,如DOM元素,作为一种称为“芬兰符号”的约定,类似于匈牙利符号。
这里有一个很好的短视频解释: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:
美元符号($)和下划线(_)允许出现在IdentifierName中的任何位置。
进一步阅读可以在http://www.ecma-international.org/ecma-262/5.1/#sec-7.6上找到
Ecma国际是成立于1961年的行业协会,致力于信息通信技术(ICT)和消费电子产品(CE)的标准化。
在你的例子中,$除了作为名称的一个字符外没有任何特殊的意义。
然而,在ECMAScript 6 (ES6)中,$可以表示模板文字
var user = 'Bob'
console.log(`We love ${user}.`); //Note backticks
// We love Bob.
$符号是变量和函数的标识符。
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
变量中的'$'对于解释器来说没有任何特殊意义,就像下划线一样。
据我所知,许多使用jQuery的人(在我看来,您的示例代码就是这样的)倾向于在包含jQuery对象的变量前面加上$,这样它们就很容易被识别,不会与整数混淆。
jQuery中的美元符号函数$()是一个经常使用的库函数,因此最好使用较短的名称。