Skip to content

Instantly share code, notes, and snippets.

@nairihar
Created April 28, 2018 11:42
Show Gist options
  • Save nairihar/11c8e509894f7a21a6923facee62c694 to your computer and use it in GitHub Desktop.
Save nairihar/11c8e509894f7a21a6923facee62c694 to your computer and use it in GitHub Desktop.
Privates in JavaScript Classes, medium, using closures, es6
// private variables
let _balance = 0
let _logs = []
export default class Bank {
constructor() {
// public variables
this.name = 'B7'
}
// public methods
getBalance() {
return _balance
}
addToBalance(count) {
_addBalance(count)
return this.getBalance()
}
}
// private methods
const _addBalance = (count) => {
_logBankChange('add', count)
_balance += count
}
const _logBankChange = (type, count) => {
_logs.push({ type, count })
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment