Skip to content

Commit

Permalink
Merge pull request ryanmcdermott#183 from DavidVujic/getters-setters
Browse files Browse the repository at this point in the history
getters and setters as functions, also without the get/set keywords
  • Loading branch information
ryanmcdermott authored Feb 2, 2017
2 parents ac6a51d + 69aac04 commit b82b63b
Showing 1 changed file with 26 additions and 33 deletions.
59 changes: 26 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -927,9 +927,7 @@ inventoryTracker('apples', req, 'www.inventory-awesome.io');

## **Objects and Data Structures**
### Use getters and setters
JavaScript doesn't have interfaces or types so it is very hard to enforce this
pattern, because we don't have keywords like `public` and `private`. As it is,
using getters and setters to access data on objects is far better than simply
Using getters and setters to access data on objects could be better than simply
looking for a property on an object. "Why?" you might ask. Well, here's an
unorganized list of reasons why:

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


**Bad:**
```javascript
class BankAccount {
constructor() {
this.balance = 1000;
}
}
function makeBankAccount() {
// ...
const bankAccount = new BankAccount();
return {
balance: 0,
// ...
};
}
// Buy shoes...
bankAccount.balance -= 100;
const account = makeBankAccount();
account.balance = 100;
```

**Good:**
```javascript
class BankAccount {
constructor(balance = 1000) {
this._balance = balance;
}
function makeBankAccount() {
// this one is private
let balance = 0;
// It doesn't have to be prefixed with `get` or `set` to be a getter/setter
set balance(amount) {
if (this.verifyIfAmountCanBeSetted(amount)) {
this._balance = amount;
}
// a "getter", made public via the returned object below
function getBalance() {
return balance;
}
get balance() {
return this._balance;
// a "setter", made public via the returned object below
function setBalance(amount) {
// ... validate before updating the balance
balance = amount;
}
verifyIfAmountCanBeSetted(val) {
return {
// ...
}
getBalance,
setBalance,
};
}
const bankAccount = new BankAccount();
// Buy shoes...
bankAccount.balance -= shoesPrice;
// Get balance
let balance = bankAccount.balance;
const account = makeBankAccount();
account.setBalance(100);
```
**[ back to top](#table-of-contents)**

Expand Down

0 comments on commit b82b63b

Please sign in to comment.