Skip to content

Commit c9bcc7e

Browse files
authored
Remove leading underscores for private properties
- It follows [ES6 styleguide](https://github.com/airbnb/javascript#naming--leading-underscore) - eslint plugin is configured this way : `'no-underscore-dangle': [2, { allowAfterThis: false }],`
1 parent 17b773e commit c9bcc7e

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

es5/README.md

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -717,7 +717,7 @@
717717
function getType() {
718718
console.log('fetching type...');
719719
// set the default type to 'no type'
720-
var type = this._type || 'no type';
720+
var type = this.type || 'no type';
721721
722722
return type;
723723
}
@@ -727,7 +727,7 @@
727727
console.log('fetching type...');
728728
729729
// set the default type to 'no type'
730-
var type = this._type || 'no type';
730+
var type = this.type || 'no type';
731731
732732
return type;
733733
}
@@ -1178,18 +1178,21 @@
11781178
});
11791179
```
11801180
1181-
- Use a leading underscore `_` when naming private properties.
1181+
- Do not use trailing or leading underscores.
1182+
1183+
> 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.
11821184
11831185
```javascript
11841186
// bad
11851187
this.__firstName__ = 'Panda';
11861188
this.firstName_ = 'Panda';
1189+
this._firstName = 'Panda';
11871190

11881191
// good
1189-
this._firstName = 'Panda';
1192+
this.firstName = 'Panda';
11901193
```
11911194
1192-
- When saving a reference to `this` use `_this`.
1195+
- Don't save references to this. Use Function#bind.
11931196
11941197
```javascript
11951198
// bad
@@ -1208,13 +1211,20 @@
12081211
};
12091212
}
12101213

1211-
// good
1214+
// bad
12121215
function () {
12131216
var _this = this;
12141217
return function () {
12151218
console.log(_this);
12161219
};
12171220
}
1221+
1222+
// good
1223+
function () {
1224+
return function () {
1225+
console.log(this);
1226+
}.bind(this);
1227+
}
12181228
```
12191229
12201230
- Name your functions. This is helpful for stack traces.

0 commit comments

Comments
 (0)