I have a JavaScript app which uses a large amount of global variables (as in, several hundred of them - it's a port of a C program so I didn't write it like that). Obviously this is very bad practice to have so many variables in the Window object, but I'm struggling to come up with a non-verbose way of doing this.
Currently I am thinking this is the way forward:
var myApp = {};
//file1.js:
myApp.variables = (function() {
var var1, var2, var3;
var items = new exampleConstructorFunction();
var etcereta = [];
})();
//file2.js:
myApp.gameplay = (function() {
//gameplay code
})();
//file3.js:
myApp.scheduling = (function() {
//timing code
})();
But the problem is, that in order to access any of the variables in myApp.variables
from functions defined in gameplay
or timing
, you would need to specify it as myApp.variables.var1
, which is pretty bad for readability when you need to use them often. Any advice?