Search code examples
javascriptbrowserscopeglobalnamed-scope

How to allow JS script to use certain variables


I'm trying to make game, where you have to program AI for your bot. I know you can access variable from your code with whole map window.myGame.data.map and you can modify it, for example, erase other bots. How can I prevent this? Is there sandbox mode that script can use just methods of an Allowed object? Is it a good idea to change window variable to custom Object? Thanks for any idea.


Solution

  • You can hide variables in JavaScript by means of closures. There is a good explanation how to do this here: http://javascript.crockford.com/private.html.

    With ES6, you can also use classes, that will also make use of closures to hide information, but it will be abstracted.