为什么 javascript 允许 $ 作为函数名称?
我开始喜欢 jQuery,但作为 PHP 开发人员,我讨厌使用它,
$().stuff
因为每个人都知道 PHP 中的 $
标记变量。我猜 javascript 中的 jQuery 声明了一个像这样的函数
function $(element) {/*stuff*/}
javascript 是否允许 $
作为函数的名称?这是一个好的行为吗?
我知道只有标准的 [0-9a-zA-Z_]
可以是 funcs/vars 的名称。
我错过了什么吗?
谢谢
I am starting to love jQuery but as PHP developer I hate using that
$().stuff
as everyone know $
in PHP marks variables. I guess jQuery in javascript declares a function like this
function $(element) {/*stuff*/}
Does javascripts permits $
as the name of a func? Is that a good behaviour?
I knew only standard [0-9a-zA-Z_]
could be the name of funcs/vars.
Am I missing something?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 PHP 中使用
$
是一个彻头彻尾的错误,这也是 Perl 的发明者所犯的。变量前缀在成熟的计算机语言中是多余的,因为像foo
和bar
这样的标识符可以被假定为没有限定的变量 - 还有什么会标识符可以识别吗? :-) 问题是发明这些语言的业余爱好者非常熟悉 Unix shell,他们显然没有意识到位于 shell 变量引用前面的$
,例如$ PATH
并不是一个漂亮的语言功能,而是一个丑陋的必需品,因为在 shell 脚本中,单词可以正常地“以其本身”出现,就像您说的那样:换句话说,像 < 这样的转义字符code>$ 表示“这是一个变量!”在像 shell 这样的文本替换语言中是必要的,并且转义对于 TCL 之类的东西也很重要,当然,在现代模板语言中,你必须能够区分所有正常的除了引入动态信息的变量引用和循环之外,还应复制到网页中的文本。
但普通的编程语言不通过文本替换来工作,因此 Perl 和 PHP 中的
$
是粗糙和噪音的。由于
$
在 JavaScript 中没有其他用途,因此是否将其用作变量的“普通字母”只是一个简单的审美判断。 JavaScript 说“是”,Python 说“否”,我已经习惯了这两种选择。我猜现在对我来说“看起来像 JavaScript”,因为$
是有效的!但在任何情况下,您都不应该让 Perl 和 PHP 的可怕选择影响您的审美判断,这是错误的选择!相反,问问自己
$
看起来是否足够像大写的S
以至于你可以认为它“足够接近一个字母”,或者它看起来是否永远像一个符号你。这才是真正的选择。The use of
$
in PHP was a flat-out mistake, which was also made by the inventor of Perl. Variable prefixes are redundant in a full-fledged computer language because identifiers likefoo
andbar
can be assumed to be variables without qualification — what else would an identifier be identifying? :-) 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: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.But normal programming languages do not work by text substitution, so the
$
in Perl and PHP is cruft and noise.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 said “yes”, Python said “no”, and I have gotten used to both choices. I guess it now “looks like JavaScript” to me for$
to be valid!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! Instead, ask yourself whether
$
looks enough like a capitalS
that you can consider it to be “close enough to a letter”, or whether it looks forever like a symbol to you. That's the real choice.来自 MDC 文档:
我认为这里的关键概念是,在 Javascript 中,函数是一流的。这意味着它们是对象,就像其他一切一样。用于引用函数的名称实际上是一个变量。
因此,作为示例,您可以执行以下操作:
然后将函数调用为
myFunc()
。由于
$
是标识符名称中的有效字符,因此它是函数的有效名称,就像包含其他内容的变量一样。在我看来,使用
$
作为 sigil PHP 中的每个变量都是愚蠢的。From the MDC docs:
I think the key concept here is that, in Javascript, functions are first-class. 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:
and then call the function as
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.是的,这是有效的。 Crockford 讨厌它,其他人喜欢它。如果您不喜欢它,请不要使用“$”,而使用“jQuery”。您甚至可以将 jQuery 置于无冲突模式,这样 $ 永远不会被设置。也就是说,如果您通过 PHP 编写了足够多的 JS,导致出现问题,那么您可能应该考虑学习如何编写更好、更通用的 JS。
Yes, it's valid. Crockford hates it, others love it. If you don't like it, don't use '$', use 'jQuery'. You can even put jQuery into no conflict mode so that $ never gets set. 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.
Javascript 只允许更多的函数名称。 $ 是函数的有效名称,$ 没有像 php 中那样的特殊含义。所以 $ 是函数的正确名称。
Javascript just allows more then just a-z for function names. $ is a valid name for a function and $ has no special meaning like in php. So $ is a correct name for a function.
您现在可以
使用
whatever(..)
而不是$(...)
文档,位于 http://api.jquery.com/jQuery.noConflict/
并引用 值、变量和文字
You can do
now you can use
whatever(..)
instead of$(...)
documentation at http://api.jquery.com/jQuery.noConflict/
and quoting Values, Variables, and Literals