@@ -927,9 +927,7 @@ inventoryTracker('apples', req, 'www.inventory-awesome.io');
|
927 | 927 |
|
928 | 928 | ## **Objects and Data Structures**
|
929 | 929 | ### Use getters and setters
|
930 |
| -JavaScript doesn't have interfaces or types so it is very hard to enforce this |
931 |
| -pattern, because we don't have keywords like `public` and `private`. As it is, |
932 |
| -using getters and setters to access data on objects is far better than simply |
| 930 | +Using getters and setters to access data on objects could be better than simply |
933 | 931 | looking for a property on an object. "Why?" you might ask. Well, here's an
|
934 | 932 | unorganized list of reasons why:
|
935 | 933 |
|
@@ -938,56 +936,51 @@ to look up and change every accessor in your codebase.
|
938 | 936 | * Makes adding validation simple when doing a `set`.
|
939 | 937 | * Encapsulates the internal representation.
|
940 | 938 | * Easy to add logging and error handling when getting and setting.
|
941 |
| -* Inheriting this class, you can override default functionality. |
942 | 939 | * You can lazy load your object's properties, let's say getting it from a
|
943 | 940 | server.
|
944 | 941 |
|
945 | 942 |
|
946 | 943 | **Bad:**
|
947 | 944 | ```javascript
|
948 |
| -class BankAccount { |
949 |
| -constructor() { |
950 |
| -this.balance = 1000; |
951 |
| -} |
952 |
| -} |
| 945 | +function makeBankAccount() { |
| 946 | +// ... |
953 | 947 |
|
954 |
| -const bankAccount = new BankAccount(); |
| 948 | +return { |
| 949 | +balance: 0, |
| 950 | +// ... |
| 951 | +}; |
| 952 | +} |
955 | 953 |
|
956 |
| -// Buy shoes... |
957 |
| -bankAccount.balance -= 100; |
| 954 | +const account = makeBankAccount(); |
| 955 | +account.balance = 100; |
958 | 956 | ```
|
959 | 957 |
|
960 | 958 | **Good:**
|
961 | 959 | ```javascript
|
962 |
| -class BankAccount { |
963 |
| -constructor(balance = 1000) { |
964 |
| -this._balance = balance; |
965 |
| -} |
| 960 | +function makeBankAccount() { |
| 961 | +// this one is private |
| 962 | +let balance = 0; |
966 | 963 |
|
967 |
| -// It doesn't have to be prefixed with `get` or `set` to be a getter/setter |
968 |
| -set balance(amount) { |
969 |
| -if (this.verifyIfAmountCanBeSetted(amount)) { |
970 |
| -this._balance = amount; |
971 |
| -} |
| 964 | +// a "getter", made public via the returned object below |
| 965 | +function getBalance() { |
| 966 | +return balance; |
972 | 967 | }
|
973 | 968 |
|
974 |
| -get balance() { |
975 |
| -return this._balance; |
| 969 | +// a "setter", made public via the returned object below |
| 970 | +function setBalance(amount) { |
| 971 | +// ... validate before updating the balance |
| 972 | +balance = amount; |
976 | 973 | }
|
977 | 974 |
|
978 |
| -verifyIfAmountCanBeSetted(val) { |
| 975 | +return { |
979 | 976 | // ...
|
980 |
| -} |
| 977 | +getBalance, |
| 978 | +setBalance, |
| 979 | +}; |
981 | 980 | }
|
982 | 981 |
|
983 |
| -const bankAccount = new BankAccount(); |
984 |
| - |
985 |
| -// Buy shoes... |
986 |
| -bankAccount.balance -= shoesPrice; |
987 |
| - |
988 |
| -// Get balance |
989 |
| -let balance = bankAccount.balance; |
990 |
| - |
| 982 | +const account = makeBankAccount(); |
| 983 | +account.setBalance(100); |
991 | 984 | ```
|
992 | 985 | **[⬆ back to top](#table-of-contents)**
|
993 | 986 |
|
|
0 commit comments