Skip to content

Instantly share code, notes, and snippets.

@eyston
Created January 29, 2012 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save eyston/1701339 to your computer and use it in GitHub Desktop.
Save eyston/1701339 to your computer and use it in GitHub Desktop.
var account = (function(startingBalance) {
var balance = startingBalance;
function getBalance() {
return balance;
}
function deposit(amount) {
balance += amount;
}
function withdraw(amount) {
balance -= amount;
}
return {
getBalance: getBalance,
deposit: deposit,
withdraw: withdraw
};
});
var accountOne = account(0);
var accountTwo = account(100);
console.log(accountOne.getBalance()); // 0
console.log(accountTwo.getBalance()); // 100
accountOne.deposit(50);
console.log(accountOne.getBalance()); // 50
console.log(accountTwo.getBalance()); // 100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment