为什么有些属性名称在 JavaScript 中以双下划线开头?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6547293/
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
Why some attribute names start with double underscore in JavaScript?
提问by Saeed Neamati
I see some attributes of some objects in JavaScript start with double underscore. For example, something like __defineGetter__
or __defineSetter__
or __proto__
.
Is it a convention defined ECMAScript specification? Or maybe it's just a convention in the developer community?
我看到 JavaScript 中某些对象的某些属性以双下划线开头。例如,类似__defineGetter__
或__defineSetter__
或__proto__
。它是一个约定定义的 ECMAScript 规范吗?或者这只是开发者社区的一个约定?
采纳答案by Alexander Bird
These are properties defined by the specific browserand are not defined by ECMAScript.
这些是由特定浏览器定义的属性,而不是由 ECMAScript 定义的。
Therefore, name collisionneeds to be avoided. If they called the property defineGetter
, then there would be no guarantee that the website's code didn't already define a property by that same name -- and that would cause many problems. However, appending two underscores has become the defacto way to define browser specific properties (since it's much less likely some website will use that convention).
因此,需要避免名称冲突。如果他们调用了 property defineGetter
,则不能保证网站的代码没有定义同名的属性——这会导致许多问题。但是,附加两个下划线已成为定义浏览器特定属性的实际方式(因为某些网站使用该约定的可能性要小得多)。
You may notice that other browsers start using the same naming convention as others (like using __proto__
), but that's still not universally guaranteed between all browsers (eg, IE does not define the __proto__ property
).
您可能会注意到其他浏览器开始使用与其他浏览器相同的命名约定(例如使用__proto__
),但这仍然不能在所有浏览器之间普遍保证(例如,IE 没有定义__proto__ property
)。
Also: the convention of using two underscores for "system-defined" identifiers (as opposed to programmer-defined identifiers) harkens back a long time, so I don't know when that convention "started" -- At least as long as C++ (see http://en.wikipedia.org/wiki/Name_mangling#Simple_example)
另外:使用两个下划线作为“系统定义”标识符(而不是程序员定义的标识符)的约定由来已久,所以我不知道该约定何时“开始”——至少与 C++ 一样长(见http://en.wikipedia.org/wiki/Name_mangling#Simple_example)
回答by Aliostad
This is so that name collisionwould be very unlikely.
这样就不太可能发生名称冲突。
JavaScript has this problem with global namespace which everyone can change or access anything. There are some data hiding techniques but sometimes will not work.
JavaScript 的全局命名空间存在这个问题,每个人都可以更改或访问任何内容。有一些数据隐藏技术,但有时不起作用。
For example if you do this, your jquery will stop working:
例如,如果您这样做,您的 jquery 将停止工作:
$ = "somethingElse";
回答by user5532169
I believe the answer needs an update. These names are now part of ECMA-262, so yes, the naming convention is now defined in ECMAScript specification.
我相信答案需要更新。这些名称现在是 ECMA-262 的一部分,所以是的,命名约定现在在 ECMAScript 规范中定义。
Starting from ECMAScript 2015 (ES6), the following has been standardized*:
从 ECMAScript 2015 (ES6) 开始,以下已标准化*:
Starting from ECMAScript 2017 (ES8), the following has been standardized*:
从 ECMAScript 2017 (ES8) 开始,以下已标准化*:
- B.2.2.2 Object.prototype.
__defineGetter__
( P, getter) - B.2.2.3 Object.prototype.
__defineSetter__
( P, setter) - B.2.2.4 Object.prototype.
__lookupGetter__
( P) - B.2.2.5 Object.prototype.
__lookupSetter__
( P)
- B.2.2.2 对象.原型。
__defineGetter__
( P,吸气剂) - B.2.2.3 对象.原型。
__defineSetter__
( P,二传手) - B.2.2.4 对象.原型。
__lookupGetter__
(P) - B.2.2.5 对象.原型。
__lookupSetter__
(P)
*: It should be noted however, the sections listed above are all defined in Annex B "Additional ECMAScript Features for Web Browsers", and a note accompanying Annex B reads (emphasis mine)
*:然而,应该注意的是,上面列出的部分都在附件 B“Web 浏览器的附加 ECMAScript 功能”中定义,附件 B 附带的注释读取(强调我的)
This annex describes various legacy features and other characteristics of web browser based ECMAScript implementations. All of the language features and behaviours specified in this annex have one or more undesirable characteristics and in the absence of legacy usage would be removed from this specification.However, the usage of these features by large numbers of existing web pages means that web browsers must continue to support them. The specifications in this annex define the requirements for interoperable implementations of these legacy features.
These features are not considered part of the core ECMAScript language. Programmers should not use or assume the existence of these features and behaviourswhen writing new ECMAScript code. ECMAScript implementations are discouraged from implementing these featuresunless the implementation is part of a web browser or is required to run the same legacy ECMAScript code that web browsers encounter.
本附件描述了基于 Web 浏览器的 ECMAScript 实现的各种遗留特性和其他特性。本附件中规定的所有语言特性和行为都有一个或多个不受欢迎的特征,如果没有遗留用法,将从本规范中删除。然而,大量现有网页对这些功能的使用意味着网络浏览器必须继续支持它们。本附件中的规范定义了这些遗留特性的可互操作实现的要求。
这些特性不被视为核心 ECMAScript 语言的一部分。程序员在编写新的 ECMAScript 代码时不应使用或假设这些特性和行为的存在。不鼓励ECMAScript实现实现这些功能,除非该实现是 Web 浏览器的一部分,或者需要运行 Web 浏览器遇到的相同旧版 ECMAScript 代码。
effectively deprecating their usage.
有效地弃用它们的用法。