File tree
Expand file treeCollapse file tree1 file changed
+33
-0
lines changed Expand file treeCollapse file tree1 file changed
+33
-0
lines changed Original file line number | Diff line number | Diff line change |
---|
@@ -946,6 +946,39 @@ const bankAccount = new BankAccount();
|
946 | 946 |
|
947 | 947 | // Buy shoes...
|
948 | 948 | 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 | + |
949 | 982 | ```
|
950 | 983 | **[⬆ back to top](#table-of-contents)**
|
951 | 984 |
|
|
You can’t perform that action at this time.
0 commit comments