Skip to content

Instantly share code, notes, and snippets.

@nairihar
Last active April 29, 2018 06:34
Show Gist options
  • Save nairihar/d25f3574aa36f111c3337d7c658b662d to your computer and use it in GitHub Desktop.
Save nairihar/d25f3574aa36f111c3337d7c658b662d to your computer and use it in GitHub Desktop.
Privates in JavaScript Classes, medium, using closures, es5
function Bank() {
// public variables
this.name = 'B7'
// public methods
this.getBalance = function() {
return _balance
}
this.addToBalance = function(count) {
_addBalance(count)
return this.getBalance()
}
// private variables
var _balance = 0
var _logs = []
// private methods
function _addBalance(count) {
_logBankChange('add', count)
_balance += count
}
function _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