简体   繁体   English

为什么javascript允许$作为函数名称?

[英]why javascript permits $ for name of functions?

I am starting to love jQuery but as PHP developer I hate using that 我开始喜欢jQuery,但作为PHP开发人员,我讨厌使用它

$().stuff

as everyone know $ in PHP marks variables. 众所周知,PHP中的$标记变量。 I guess jQuery in javascript declares a function like this 我猜javascript中的jQuery声明了这样的函数

function $(element) {/*stuff*/}

Does javascripts permits $ as the name of a func? javascript是否允许将$作为函数的名称? Is that a good behaviour? 这是好行为吗?

I knew only standard [0-9a-zA-Z_] could be the name of funcs/vars. 我知道只有标准[0-9a-zA-Z_]是funcs / vars的名称。

Am I missing something? 我想念什么吗?

Thanks 谢谢

The use of $ in PHP was a flat-out mistake, which was also made by the inventor of Perl. 在PHP中使用$是一个明显的错误,这也是Perl的发明者犯的。 Variable prefixes are redundant in a full-fledged computer language because identifiers like foo and bar can be assumed to be variables without qualification — what else would an identifier be identifying? 变量的前缀是一个完整的计算机语言多余的,因为像标识符foobar可以被认为是无资质的变量-还有什么的标识符来识别? :-) The problem is that the amateurs who invented these languages were very familiar with the Unix shell, and they apparently did not realize that the $ that goes in front of shell variable references like $PATH is not a beautiful language feature, but an ugly necessity imposed by the fact that, in a shell script, words can appear normally “as themselves”, as when you say: :-)问题是,谁发明了这些语言的爱好者都非常熟悉Unix外壳,他们显然没有意识到, $是去像shell变量引用前面的$PATH是不是一个美丽的语言功能,但一个在shell脚本中单词通常可以“本身”出现的事实,例如在您说以下情况时,这是非常必要的:

$ echo PATH
PATH
$ echo $PATH
/usr/local/bin:/usr/bin:/bin

In other words, an escape character like $ that says “this is a variable!” is necessary in a text substitution language like the shell, and escaping is also important in things like TCL and, of course, in modern templating languages, where you have to be able to tell apart all of the normal text that should be copied into the web page apart from the variable references and loops that pull in dynamic information. 换句话说,在诸如shell之类的文本替换语言中,必须使用$这样的转义字符表示“这是一个变量!”,而转义在TCL之类的东西中也很重要,当然,在现代模板语言中,必须能够分辨应复制到网页中的所有普通文本,除了引入动态信息的变量引用和循环。

But normal programming languages do not work by text substitution, so the $ in Perl and PHP is cruft and noise. 但是普通的编程语言不能通过文本替换来工作,因此Perl和PHP中的$显得有些杂乱无章。

Since $ has no other use in JavaScript, it was a simple aesthetic judgement about whether to make it available as a “normal letter” for variables. 由于$在JavaScript中没有其他用途,因此对于是否将其用作变量的“常规字母”,这是一个简单的美学判断。 JavaScript said “yes”, Python said “no”, and I have gotten used to both choices. JavaScript说“是”,Python说“否”,我已经习惯了这两种选择。 I guess it now “looks like JavaScript” to me for $ to be valid! 我想对于$来说,现在对我来说“看起来像JavaScript”!

But in no case should you let your aesthetic judgement here be informed by the horrible choices of Perl and PHP, which were wrong choices! 但是在任何情况下,都不要让Perl和PHP的错误选择引起您的审美判断,这是错误的选择! Instead, ask yourself whether $ looks enough like a capital S that you can consider it to be “close enough to a letter”, or whether it looks forever like a symbol to you. 相反,请问自己, $是否看起来像大写的S ,可以认为它“足够接近字母”,或者对您而言,它永远看起来像是一个符号。 That's the real choice. 那才是真正的选择。

Yes, it's valid. 是的,这是有效的。 Crockford hates it , others love it. 克罗克福德(Crockford)讨厌它 ,其他人喜欢它。 If you don't like it, don't use '$', use 'jQuery'. 如果您不喜欢它,请不要使用“ $”,请使用“ jQuery”。 You can even put jQuery into no conflict mode so that $ never gets set. 您甚至可以将jQuery置于无冲突模式,这样$就永远不会被设置。 That said, if you're writing enough JS via PHP that it's causing you problems, you should probably consider learning how to write better, more generic JS. 就是说,如果您通过PHP编写了足够多的JS,这会引起问题,那么您应该考虑学习如何编写更好的,更通用的JS。

From the MDC docs : MDC文档

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); JavaScript标识符必须以字母,下划线(_)或美元符号($)开头; subsequent characters can also be digits (0-9). 后续字符也可以是数字(0-9)。 Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase). 因为JavaScript区分大小写,所以字母包括字符“ A”至“ Z”(大写)和字符“ a”至“ z”(小写)。

I think the key concept here is that, in Javascript, functions are first-class. 我认为这里的关键概念是,在Javascript中,函数是一流的。 This means that they are objects, just like everything else. 这意味着它们是对象,就像其他所有对象一样。 The name you use to refer to a function is effectively a variable. 您用来引用函数的名称实际上是一个变量。

So, as an example, you can do this: 因此,作为示例,您可以执行以下操作:

var myFunc = function() { /* some code */ };

and then call the function as myFunc() . 然后将函数调用为myFunc()

Since $ is a valid character in identifier names, it is a valid name for a function just as much as a variable containing anything else. 由于$是标识符名称中的有效字符,因此它是函数的有效名称,与包含其他内容的变量一样多。

In my opinion, the use of $ as a sigil for every variable in PHP is bone-headed. 在我看来,使用$作为印记每一个 PHP变量是骨领导。

Javascript just allows more then just az for function names. Java语言只允许使用az作为函数名。 $ is a valid name for a function and $ has no special meaning like in php. $是函数的有效名称,$没有像php那样的特殊含义。 So $ is a correct name for a function. 因此,$是函数的正确名称。

You can do 你可以做

var whatever = $.noConflict();

now you can use whatever(..) instead of $(...) 现在您可以使用whatever(..)代替$(...)

documentation at http://api.jquery.com/jQuery.noConflict/ http://api.jquery.com/jQuery.noConflict/中的文档


and quoting Values, Variables, and Literals 并引用值,变量和文字

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); JavaScript标识符必须以字母,下划线(_)或美元符号($)开头; subsequent characters can also be digits (0-9). 后续字符也可以是数字(0-9)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM