开发者

Why some attribute names start with double underscore in JavaScript?

I see some attributes of som开发者_StackOverflowe 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?


These are properties defined by the specific browser and are not defined by ECMAScript.

Therefore, name collision needs 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).

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

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 )


This is so that name collision would 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.

For example if you do this, your jquery will stop working:

$ = "somethingElse";


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.

Starting from ECMAScript 2015 (ES6), the following has been standardized*:

  • B.2.2.1 Object.prototype.__proto__

Starting from ECMAScript 2017 (ES8), the following has been standardized*:

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

*: 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)

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 behaviours when writing new ECMAScript code. ECMAScript implementations are discouraged from implementing these features unless the implementation is part of a web browser or is required to run the same legacy ECMAScript code that web browsers encounter.

effectively deprecating their usage.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜