简体   繁体   中英

Modifying variables in a closure

My focus in this question is making HTML5 games. The aim is to help reduce/prevent cheating.

If a variable in global scope held the score, for example var score = 0 , then it's really easy to cheat just by running javascript:void(score = 9999999999); .

But if I had something more like this:

(function() {
    var score = 0;
    // game logic here
})();

Is score accessible by anything outside the closure? Is there any way for the player to modify it and thus falsify their points?

Everything that happens on the client side is 'hackable', because you simply do not have any control over it. The only real way to prevent this is using serverside validation (eg AJAX calls).

Is score accessible by anything outside the closure?

No it isn't.

Is there any way for the player to modify it and thus falsify their points?

Yup. See my first statement.

Outside of that closure, score does not exist (or at least, that score doesn't).

If you want to prevent cheaters, the only real way when the whole game runs on the client side is to recreate the game environment and user input on the server and validate that.

Otherwise, anyone can modify your client side code and send what they wish to your server.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM