Is the underscore prefix for property and method names merely a convention?

javascript, naming-conventions, scope

Solution

Welcome to 2019!

It appears a proposal to extend class syntax to allow for `#` prefixed variable to be private was accepted. Chrome 74 ships with this support.

`_` prefixed variable names are considered private by convention but are still public.

This syntax tries to be both terse and intuitive, although it's rather different from other programming languages.

Why was the sigil # chosen, among all the Unicode code points?

- @ was the initial favorite, but it was taken by decorators. TC39 considered swapping decorators and private state sigils, but the committee decided to defer to the existing usage of transpiler users.

- _ would cause compatibility issues with existing JavaScript code, which has allowed _ at the start of an identifier or (public) property name for a long time.

This proposal reached Stage 3 in July 2017. Since that time, there has been extensive thought and lengthy discussion about various alternatives. In the end, this thought process and continued community engagement led to renewed consensus on the proposal in this repository. Based on that consensus, implementations are moving forward on this proposal.

See https://caniuse.com/#feat=mdn-javascript_classes_private_class_fields

Problem

Is the underscore prefix in JavaScript only a convention, like for example in Python private class methods are? From the 2.7 Python documentation: “Private” instance variables that cannot be accessed except from inside an object don’t exist in Python. However, there is a convention that is followed by most Python code: a name prefixed with an underscore (e.g. _spam) should be treated as a non-public part of the API (whether it is a function, a method or a data member). Does this also apply to JavaScript? Take for example this JavaScript code: ``` function AltTabPopup() { this._init(); } AltTabPopup.prototype = { _init : function() { ... } } ``` Also, underscore prefixed variables are used. ``` ... this._currentApp = 0; this._currentWindow = -1; this._thumbnailTimeoutId = 0; this._motionTimeoutId = 0; ... ``` Only conventions? Or is there more behind the underscore prefix? I admit my question is quite similar to this question, but it didn't make one smarter about the significance of the underscore prefix in JavaScript.

Original source

Related problems