简体   繁体   English

JavaScript中使用的$符号是什么?

[英]What is the $ symbol used for in JavaScript

I am a JavaScript learner and have been researching this matter, but with no success. 我是一名JavaScript学习者,一直在研究这个问题,但没有成功。 What is the $ symbol used for in JavaScript besides regular expressions? 除了正则表达式之外,JavaScript中使用的$符号是什么? Any resources or readings regarding this would be appreciated. 任何有关此的资源或读数将不胜感激。 Thanks . 谢谢

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". 例如,如果你正在使用jQuery并且你说$("div") ,那么这是一个带参数“div”的$函数调用。 When you say $.post() , it's calling the post method on the $ object (Javascript is nice in that functions are first-class objects). 当你说$.post() ,它在$对象上调用post方法(Javascript很好,因为函数是第一类对象)。

I became acquainted with it in JavaScript when I started using the Prototype framework. 当我开始使用Prototype框架时,我在JavaScript中熟悉它。 In Prototype, $ is simply the name of an often used function (very, very much simplified - a short for document.getElementById ). 在Prototype中, $只是常用函数的名称(非常非常简化 - document.getElementById的简称)。 Personally, I like the terseness of it. 就个人而言,我喜欢它的简洁性。

Afaik, it's not used for anything by the language itself. Afaik,它不被语言本身用于任何东西。

For what it's worth, Douglas Crockford advises against using $ in the variable/function names you write: 对于它的价值, 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. 我的意思是,看一下带有美元符号的程序。

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. 在使用jQuery的代码中, $通常用作包含jQuery选择的变量的前缀。 eg var $container = $('.container'); 例如var $container = $('.container'); .

In AngularJS , they use the $ prefix to mean "core Angular functionality". AngularJS中 ,他们使用$前缀来表示“核心角度功能”。 That way, you know which methods and services are added by the framework, and which are custom to your application. 这样,您就知道框架添加了哪些方法和服务,以及哪些方法和服务是您的应用程序自定义的。

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

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