JavaScript 中 $ 符号的作用是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3107543/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the $ symbol used for in JavaScript
提问by Babiker
I am a JavaScript learner and have been researching this matter, but with no success. What is the $symbol used for in JavaScript besides regular expressions? Any resources or readings regarding this would be appreciated. Thanks.
我是一名 JavaScript 学习者,一直在研究这个问题,但没有成功。$除了正则表达式之外,JavaScript 中使用的符号是什么?任何与此相关的资源或阅读资料将不胜感激。 谢谢。
回答by Dean Harding
It doesn't mean anything special.
这并不意味着什么特别。
But because $is allowed in identifier names, many Javascript libraries have taken to using $as the "central" interface to them, or at least as a shortcut for accessing their functionality.
但是由于$标识符名称中允许使用,许多 Javascript 库已将其$用作它们的“中央”接口,或者至少用作访问其功能的快捷方式。
For example, if you're using jQuery and you say $("div"), this is a call to the $function with argument "div". When you say $.post(), it's calling the postmethod on the $object (Javascript is nice in that functions are first-class objects).
例如,如果您正在使用 jQuery 并且您说$("div"),这是对$带有参数“div”的函数的调用。当您说 时$.post(),它正在调用对象post上的方法$(Javascript 很好,因为函数是一流的对象)。
回答by Lauri Lehtinen
I became acquainted with it in JavaScript when I started using the Prototype framework. In Prototype, $is simply the name of an often used function (very, very much simplified - a short for document.getElementById). Personally, I like the terseness of it.
当我开始使用 Prototype 框架时,我在 JavaScript 中熟悉了它。在 Prototype 中,$只是常用函数的名称(非常非常简化 - 的缩写document.getElementById)。就个人而言,我喜欢它的简洁。
Afaik, it's not used for anything by the language itself.
Afaik,它没有被语言本身用于任何事情。
For what it's worth, Douglas Crockfordadvises against using $in the variable/function names youwrite:
就其价值而言,Douglas Crockford建议不要$在您编写的变量/函数名称中使用:
Do not use $ (dollar sign) or \ (backslash) in names.
不要在名称中使用 $(美元符号)或 \(反斜杠)。
Adding another, rather opinionated, quote from Mr. Crockford's talk "And Then There Was JavaScript":
添加另一个来自 Crockford 先生的演讲“然后有 JavaScript”的相当固执的引述:
Dollar sign was added to the language specifically for use by code generators and macro processes, so if you have machines writing code then the machines need to be confident that the variables that they create will not conflict with variables that the humans are going to create. To distinguish them, we'll allow the machines to use dollar sign. Some of the ninjas found out about that and thought oh, dollar sign, I can use dollar sign as a function name, so they're out there doing that. And it looks stupid. I mean, look at a program with dollar sign.
美元符号被添加到语言中,专门供代码生成器和宏进程使用,因此如果您有机器编写代码,那么机器需要确信它们创建的变量不会与人类将要创建的变量冲突。为了区分它们,我们将允许机器使用美元符号。一些忍者发现了这一点,并认为哦,美元符号,我可以使用美元符号作为函数名,所以他们就在那里这样做。而且看起来很蠢。我的意思是,看一个带有美元符号的程序。
回答by GregL
If you are asking why some variables and function names start with $, then that is simply a convention when using jQuery and/or AngularJS.
如果您问为什么某些变量和函数名称以 开头$,那么这只是使用 jQuery 和/或 AngularJS 时的约定。
In code that uses jQuery, $is often used as a prefix for variables that contain jQuery selections.
e.g. var $container = $('.container');.
在使用jQuery 的代码中,$通常用作包含 jQuery 选择的变量的前缀。例如var $container = $('.container');。
In AngularJS, they use the $prefix to mean "core Angular functionality". That way, you know which methods and services are added by the framework, and which are custom to your application.
在AngularJS 中,他们使用$前缀来表示“核心 Angular 功能”。这样,您就知道框架添加了哪些方法和服务,哪些是您的应用程序自定义的。

