Why some attribute names start with double underscore in JavaScript?

Saeed Neamati picture Saeed Neamati · Jul 1, 2011 · Viewed 22k times · Source

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?

Answer

Alexander Bird picture Alexander Bird · Jul 1, 2011

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 )