Skip to content

Commit 69aac04

Browse files
committed
getters and setters as functions, also without the get/set keywords
1 parent ac6a51d commit 69aac04

File tree

1 file changed

+26
-33
lines changed

1 file changed

+26
-33
lines changed

README.md

+26-33
Original file line numberDiff line numberDiff line change
@@ -927,9 +927,7 @@ inventoryTracker('apples', req, 'www.inventory-awesome.io');
927927

928928
## **Objects and Data Structures**
929929
### 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
933931
looking for a property on an object. "Why?" you might ask. Well, here's an
934932
unorganized list of reasons why:
935933

@@ -938,56 +936,51 @@ to look up and change every accessor in your codebase.
938936
* Makes adding validation simple when doing a `set`.
939937
* Encapsulates the internal representation.
940938
* Easy to add logging and error handling when getting and setting.
941-
* Inheriting this class, you can override default functionality.
942939
* You can lazy load your object's properties, let's say getting it from a
943940
server.
944941

945942

946943
**Bad:**
947944
```javascript
948-
class BankAccount {
949-
constructor() {
950-
this.balance = 1000;
951-
}
952-
}
945+
function makeBankAccount() {
946+
// ...
953947

954-
const bankAccount = new BankAccount();
948+
return {
949+
balance: 0,
950+
// ...
951+
};
952+
}
955953

956-
// Buy shoes...
957-
bankAccount.balance -= 100;
954+
const account = makeBankAccount();
955+
account.balance = 100;
958956
```
959957

960958
**Good:**
961959
```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;
966963

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;
972967
}
973968

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;
976973
}
977974

978-
verifyIfAmountCanBeSetted(val) {
975+
return {
979976
// ...
980-
}
977+
getBalance,
978+
setBalance,
979+
};
981980
}
982981

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);
991984
```
992985
**[⬆ back to top](#table-of-contents)**
993986

0 commit comments

Comments
 (0)