Skip to content

Instantly share code, notes, and snippets.

@marekkalnik
Created February 18, 2012 15:30
Show Gist options
  • Save marekkalnik/1859797 to your computer and use it in GitHub Desktop.
Save marekkalnik/1859797 to your computer and use it in GitHub Desktop.
Javascript private methods
Soldier = function() {
var shot, reload, runAway,
ammo = 10,
clips = 2;
shot = function() {
if(ammo == 0 && reload() === false) {
runAway();
} else {
ammmo -= 1;
}
};
reload = function() {
if (clips > 0) {
clips -= 1;
ammo += 10;
return true;
}
return false;
};
runAway = function() {};
// Return public methods
return {shot: shot};
};
};
privateRayan = Soldier();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment