Skip to content

Commit 0951bc2

Browse files
Merge pull request ryanmcdermott#101 from kurtommy/fix/getter-setter
Fix/getter setter
2 parents f5e59ee + ce4315f commit 0951bc2

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,39 @@ const bankAccount = new BankAccount();
946946

947947
// Buy shoes...
948948
bankAccount.withdraw(100);
949+
```
950+
951+
**Good**:
952+
```javascript
953+
class BankAccount {
954+
constructor(balance = 1000) {
955+
this._balance = balance;
956+
}
957+
958+
// It doesn't have to be prefixed with `get` or `set` to be a getter/setter
959+
set balance(amount) {
960+
if (verifyIfAmountCanBeSetted(amount)) {
961+
this._balance = amount;
962+
}
963+
}
964+
965+
get balance() {
966+
return this._balance;
967+
}
968+
969+
verifyIfAmountCanBeSetted(val) {
970+
// ...
971+
}
972+
}
973+
974+
const bankAccount = new BankAccount();
975+
976+
// Buy shoes...
977+
bankAccount.balance -= shoesPrice;
978+
979+
// Get balance
980+
let balance = bankAccount.balance;
981+
949982
```
950983
**[⬆ back to top](#table-of-contents)**
951984

0 commit comments

Comments
 (0)