Skip to content

Commit 49c041c

Browse files
committed
[guide] [breaking] Disallow leading underscores - there’s no such thing as “private properties”.
Per airbnb/javascript#490 (comment)
1 parent 7a5d4ca commit 49c041c

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -2294,15 +2294,18 @@ Other Style Guides
22942294
```
22952295
22962296
<a name="naming--leading-underscore"></a><a name="22.4"></a>
2297-
- [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)
2297+
- [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)
2298+
2299+
> 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.
22982300
22992301
```javascript
23002302
// bad
23012303
this.__firstName__ = 'Panda';
23022304
this.firstName_ = 'Panda';
2305+
this._firstName = 'Panda';
23032306

23042307
// good
2305-
this._firstName = 'Panda';
2308+
this.firstName = 'Panda';
23062309
```
23072310
23082311
<a name="naming--self-this"></a><a name="22.5"></a>

0 commit comments

Comments
 (0)