Created
March 27, 2011 14:53
-
-
Save caike/889267 to your computer and use it in GitHub Desktop.
encapsulation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | |
} | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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