Skip to content

Commit d7b8cc1

Browse files
authored
Update README.md
1 parent fcfab4e commit d7b8cc1

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

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

947947
// Buy shoes...
948948
bankAccount.withdraw(100);
949+
```
950+
951+
**Good with getter/setter**:
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 (verifyAmountCanBeSetted(amount)) {
961+
this._balance = amount;
962+
}
963+
}
964+
965+
get balance() {
966+
return this._balance;
967+
}
968+
}
969+
970+
const bankAccount = new BankAccount();
971+
972+
// Buy shoes...
973+
bankAccount.balance -= shoesPrice;
974+
975+
// Get balance
976+
let balance = bankAccount.balance;
977+
949978
```
950979
**[⬆ back to top](#table-of-contents)**
951980

0 commit comments

Comments
 (0)