Skip to content

Instantly share code, notes, and snippets.

@nairihar
Created April 28, 2018 17:07
Show Gist options
  • Save nairihar/1a9b2e9b67a0036aaeaa943cf84aef0b to your computer and use it in GitHub Desktop.
Save nairihar/1a9b2e9b67a0036aaeaa943cf84aef0b to your computer and use it in GitHub Desktop.
Privates in JavaScript Symbol, medium, using Symbol, es6
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