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

发布于 2024-10-24 01:10:02 字数 359 浏览 5 评论 0原文

我开始喜欢 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

琉璃梦幻 2024-10-31 01:10:02

在 PHP 中使用 $ 是一个彻头彻尾的错误,这也是 Perl 的发明者所犯的。变量前缀在成熟的计算机语言中是多余的,因为像 foobar 这样的标识符可以被假定为没有限定的变量 - 还有什么标识符可以识别吗? :-) 问题是发明这些语言的业余爱好者非常熟悉 Unix shell,他们显然没有意识到位于 shell 变量引用前面的 $,例如 $ PATH 并不是一个漂亮的语言功能,而是一个丑陋的必需品,因为在 shell 脚本中,单词可以正常地“以其本身”出现,就像您说的那样:

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

换句话说,像 < 这样的转义字符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 like foo and bar 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:

$ 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.

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 capital S 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.

禾厶谷欠 2024-10-31 01:10:02

来自 MDC 文档

JavaScript 标识符必须以字母、下划线 (_) 或美元符号 ($) 开头;后续字符也可以是数字 (0-9)。由于 JavaScript 区分大小写,因此字母包括字符“A”到“Z”(大写)和字符“a”到“z”(小写)。

我认为这里的关键概念是,在 Javascript 中,函数是一流的。这意味着它们是对象,就像其他一切一样。用于引用函数的名称实际上是一个变量。

因此,作为示例,您可以执行以下操作:

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

然后将函数调用为 myFunc()

由于 $ 是标识符名称中的有效字符,因此它是函数的有效名称,就像包含其他内容的变量一样。

在我看来,使用 $ 作为 sigil PHP 中的每个变量都是愚蠢的。

From the MDC docs:

A JavaScript identifier must start with a letter, underscore (_), or dollar sign ($); subsequent characters can also be digits (0-9). Because JavaScript is case sensitive, letters include the characters "A" through "Z" (uppercase) and the characters "a" through "z" (lowercase).

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:

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

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.

情话难免假 2024-10-31 01:10:02

是的,这是有效的。 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.

陌路终见情 2024-10-31 01:10:02

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.

七颜 2024-10-31 01:10:02

您现在可以

var whatever = $.noConflict();

使用 whatever(..) 而不是 $(...)

文档,位于 http://api.jquery.com/jQuery.noConflict/


并引用 值、变量和文字

必须以 JavaScript 标识符开头
带有字母、下划线 (_) 或
美元符号 ($);后续字符
也可以是数字 (0-9)。

You can do

var whatever = $.noConflict();

now you can use whatever(..) instead of $(...)

documentation at http://api.jquery.com/jQuery.noConflict/


and quoting Values, Variables, and Literals

A JavaScript identifier must start
with a letter, underscore (_), or
dollar sign ($); subsequent characters
can also be digits (0-9).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文