Created
April 28, 2018 17:07
-
-
Save nairihar/1a9b2e9b67a0036aaeaa943cf84aef0b to your computer and use it in GitHub Desktop.
Privates in JavaScript Symbol, medium, using Symbol, es6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const _addBalance = Symbol('addBalance') | |
const _logBankChange = Symbol('logBankChange') | |
const _privateData = Symbol('privateData') | |
export default class Bank { | |
constructor() { | |
// public variables | |
this.name = 'B7' | |
// private variables | |
this[_privateData] = { | |
balance: 0, | |
logs: [] | |
} | |
} | |
// public methods | |
getBalance() { | |
return this[_privateData].balance | |
} | |
addToBalance(count) { | |
this[_addBalance](count) | |
return this.getBalance() | |
} | |
// private methods | |
[_addBalance](count) { | |
this[_logBankChange]('add', count) | |
this[_privateData].balance += count | |
} | |
[_logBankChange](type, count) { | |
this[_privateData].logs.push({ type, count }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment