Skip to content

Commit d427d05

Browse files
authored
Merge pull request #19 from briisk/getters-setters
It's not okay to create get() and set() functions.
2 parents 042ad26 + 8271d1b commit d427d05

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2877,9 +2877,10 @@
28772877
```
28782878
28792879
<a name="accessors--consistent"></a><a name="23.4"></a>
2880-
- [23.4](#accessors--consistent) It's okay to create get() and set() functions, but be consistent.
2880+
- [23.4](#accessors--consistent) It's not okay to create get() and set() functions. Why? Because get and set should be used only as getters and setters.
28812881
28822882
```javascript
2883+
// bad
28832884
class Jedi {
28842885
constructor(options = {}) {
28852886
const lightsaber = options.lightsaber || 'blue';
@@ -2894,6 +2895,22 @@
28942895
return this[key];
28952896
}
28962897
}
2898+
2899+
// good
2900+
class Jedi {
2901+
constructor(options = {}) {
2902+
const lightsaber = options.lightsaber || 'blue';
2903+
this._lightsaber = lightsaber;
2904+
}
2905+
2906+
set lightsaber(val) {
2907+
this._lightsaber = val;
2908+
}
2909+
2910+
get lightsaber() {
2911+
return this._lightsaber;
2912+
}
2913+
}
28972914
```
28982915
28992916
**[⬆ back to top](#table-of-contents)**

0 commit comments

Comments
 (0)