mirror of
https://github.com/airbnb/javascript.git
synced 2026-01-14 14:27:54 -05:00
[guide] [breaking] Disallow leading underscores - there’s no such thing as “private properties”.
Per https://github.com/airbnb/javascript/issues/490#issuecomment-209126331
This commit is contained in:
@@ -2294,15 +2294,18 @@ Other Style Guides
|
||||
```
|
||||
|
||||
<a name="naming--leading-underscore"></a><a name="22.4"></a>
|
||||
- [22.4](#naming--leading-underscore) Use a leading underscore `_` when naming private properties. eslint: [`no-underscore-dangle`](http://eslint.org/docs/rules/no-underscore-dangle.html) jscs: [`disallowDanglingUnderscores`](http://jscs.info/rule/disallowDanglingUnderscores)
|
||||
- [22.4](#naming--leading-underscore) Do not use trailing or leading underscores. eslint: [`no-underscore-dangle`](http://eslint.org/docs/rules/no-underscore-dangle.html) jscs: [`disallowDanglingUnderscores`](http://jscs.info/rule/disallowDanglingUnderscores)
|
||||
|
||||
> Why? JavaScript does not have the concept of privacy in terms of properties or methods. Although a leading underscore is a common convention to mean “private”, in fact, these properties are fully public, and as such, are part of your public API contract. This convention might lead developers to wrongly think that a change won't count as breaking, or that tests aren't needed. tl;dr: if you want something to be “private”, it must not be observably present.
|
||||
|
||||
```javascript
|
||||
// bad
|
||||
this.__firstName__ = 'Panda';
|
||||
this.firstName_ = 'Panda';
|
||||
this._firstName = 'Panda';
|
||||
|
||||
// good
|
||||
this._firstName = 'Panda';
|
||||
this.firstName = 'Panda';
|
||||
```
|
||||
|
||||
<a name="naming--self-this"></a><a name="22.5"></a>
|
||||
|
||||
Reference in New Issue
Block a user