Skip to content

Instantly share code, notes, and snippets.

@caike
Created March 27, 2011 14:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save caike/889267 to your computer and use it in GitHub Desktop.
Save caike/889267 to your computer and use it in GitHub Desktop.
encapsulation
function Rifle(rounds) {
var _rounds = rounds
, _reloadingMechanism;
function _setRound(roundNumber){
_rounds = roundNumber;
}
// Encapsulation in JS (cool)
this.fire = function(){
if(_reloadingMechanism.isReloading()){
throw new Error("still loading")
}
if(_rounds > 0){
return "fire";
}else{
return "click";
}
};
}
riffle = new Rifle(2);
riffle['_rounds'];
riffle['_setRound'];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment