javascript 角度中使用的 $$(双美元符号)是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19338493/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 15:14:24  来源:igfitidea点击:

What is the $$ (double dollar sign) used for in angular?

javascriptangularjs

提问by ThinkingInBits

I'm taking a look at the angular 1.2 source code, and I'm just curious why some functions are prefixed with the two dollar signs. Is this some sort of convention?

我正在查看 angular 1.2 源代码,我很好奇为什么有些函数以两个美元符号为前缀。这是某种约定吗?

回答by Doug T.

  • Single $for reserved, public identifiers
  • Double $$for reserved privateidentifiers
  • Single$用于保留的公共标识符
  • $$保留私有标识符的双倍

To quote the docs:

引用文档

$ Prefix Naming Convention

...

If you inspect a Scope, you may also notice some properties that begin with $$. These properties are considered private, and should not be accessed or modified.

$ 前缀命名约定

...

如果您检查 Scope,您可能还会注意到一些以 $$ 开头的属性。这些属性被认为是私有的,不应访问或修改。

回答by SoEzPz

If I may add:

如果我可以补充:

Angularjs Docs

Angularjs 文档

Other than just being significant to Angularjs, the '$$' or '$' are just characters that are allowed in variable names. Angularjs uses both to identify significance for you and their own development team.

除了对 Angularjs 重要之外,'$$' 或 '$' 只是变量名中允许的字符。Angularjs 使用两者来确定对您和他们自己的开发团队的重要性。

You could name all of your variables in the same way; but to avoid naming collisions, stay away from this practice. Here are some examples if you did...

您可以以相同的方式命名所有变量;但是为了避免命名冲突,请远离这种做法。这里有一些例子,如果你这样做...

$$$$myVariableName; $myVariableName$; _myVariableName_; $$$$$$myVariableName$$$$$$$$

Here is a link to test out JS variable names if you wish:

如果您愿意,这里有一个测试 JS 变量名称的链接:

Variable Name Validator

变量名验证器

Here is a link to MDN as well that explains allowed characters:

这里还有一个指向 MDN 的链接,解释了允许的字符:

MDN allowed characters link

MDN 允许字符链接

and here is the text:

这是文本:

Variables

You use variables as symbolic names for values in your application. The names of variables, called identifiers, conform to certain rules.

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

Starting with JavaScript 1.5, you can use ISO 8859-1 or Unicode letters such as ? and ü in identifiers. You can also use the \uXXXX Unicode escape sequences as characters in identifiers.

Some examples of legal names are Number_hits, temp99, and _name.

变量

您可以使用变量作为应用程序中值的符号名称。变量的名称,称为标识符,符合一定的规则。

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

从 JavaScript 1.5 开始,您可以使用 ISO 8859-1 或 Unicode 字母,例如 ? 和 ü 在标识符中。您还可以使用 \uXXXX Unicode 转义序列作为标识符中的字符。

合法名称的一些示例是 Number_hits、temp99 和 _name。

Angulajs includes quite a bit of information in each object; some of the items are for Angularjs, and some are for the developer, which means that some may not be editable, but all should be available for reference if that is what you were going to use it for.

Angulajs 在每个对象中都包含了相当多的信息;有些项目是针对 Angularjs 的,有些是针对开发人员的,这意味着有些项目可能无法编辑,但如果这是您要使用它的目的,那么所有项目都应该可供参考。

However, in future releases any private identifiers may disappear as the Angularjs team expects the developer not to use the reserved / private names.

但是,在未来的版本中,任何私有标识符都可能会消失,因为 Angularjs 团队希望开发人员不要使用保留/私有名称。

In the case of the posted 'similar link' here is what Angularjs says:

在发布的“类似链接”的情况下,Angularjs 是这样说的:

$ Prefix Naming Convention You can create your own services, and in fact we will do exactly that in step 11. As a naming convention, Angular's built-in services, Scope methods and a few other Angular APIs have a $ prefix in front of the name.

The $ prefix is there to namespace Angular-provided services. To prevent collisions it's best to avoid naming your services and models anything that begins with a $.

If you inspect a Scope, you may also notice some properties that begin with $$. These properties are considered private, and should not be accessed or modified.

$ 前缀命名约定您可以创建自己的服务,实际上我们将在步骤 11 中做到这一点。作为命名约定,Angular 的内置服务、Scope 方法和其他一些 Angular API 在名称。

$ 前缀用于命名空间 Angular 提供的服务。为防止冲突,最好避免命名您的服务并为任何以 $ 开头的模型命名。

如果您检查 Scope,您可能还会注意到一些以 $$ 开头的属性。这些属性被认为是私有的,不应访问或修改。