0

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?

Magg G.
  • 229
  • 3
  • 10

3 Answers3

4

You could just store your current globals directly in myApp, or you could avoid that approach and wrap the entire script in an anonymous function and execute it - like (function() { })() then you don't need to namespace your variables to keep them out of global scope.

  • This sounds like a good solution. The only problem I would see is if you wanted to maintain a namespace to keep the "hundreds" of global variables from conflicting with ones you declare within the the function. – dan_paul May 10 '14 at 23:00
  • 1
    @dan_paul—that problem already exists (and presumably overcome), using an IIFE doesn't make it worse. It just moves the problem from global to function context, which is what the OP wants. – RobG May 10 '14 at 23:02
  • that would make them properties of the myApp object though, no? as far as I know, object properties can't access other object properties (could be wrong about that?). I thought about combining the whole script, but it's pretty big (so debugging won't be fun), and it separates really well so just seems cleaner to keep them namespaced – Magg G. May 10 '14 at 23:02
  • @jqueryrocks Agree. [How to avoid global variables in JavaScript?](http://stackoverflow.com/questions/1841916/how-to-avoid-global-variables-in-javascript) – Joel Allison May 10 '14 at 23:04
  • @jqueryrocs - that makes sense. I was responding to the first part of your suggestion (storing the globals direcly in `myApp`). If you don't need the myApp object than an IIFE would definitely avoid global namespace pollution. I'm assuming the OP may need this object (looks like they might be using a framework). – dan_paul May 10 '14 at 23:20
  • @dan_paul Yeah that makes sense if he needs the object –  May 10 '14 at 23:22
  • @dan_paul I don't need the object. I want to have "gameplay", "scheduling" etc (these are in separate files, btw) be able to access all of the global variables, without having to combine everything into one huge script file. That's all – Magg G. May 10 '14 at 23:33
0

If you're just concerned with the length of the variable name, couldn't you just define a reference to it in a local scope?

var v = myApp.variables;
dan_paul
  • 309
  • 1
  • 10
  • but then for every variable you would still have to write `v.varname`, rather than just `varname` – Magg G. May 10 '14 at 22:58
  • Yes, but you keep a namespace. That way if you accidentally declare `var varname = 'foo';` within your function, it won't conflict with `v.varname`. Really depends on the use case though, I guess. – dan_paul May 10 '14 at 23:02
  • but.. if varname is intended to be global in the first place, wouldn't that be the "correct" behaviour for declaring varname again? – Magg G. May 10 '14 at 23:17
  • I'm not sure I follow you 100%. I think if you don't need the myApp object, you should go with @jquerrrocks response to use an IIFE. You will have all your "global" variables available to you without having to pollute global variable space. I just read this to get up to speed: http://benalman.com/news/2010/11/immediately-invoked-function-expression/ – dan_paul May 10 '14 at 23:25
0

I use ASP, Windows server, MS SQL DB's, but this could also be done in PHP, Linux-based servers, MySQL DB's. You would need to create one or more DB's, then write an ASP script (or PHP script) to store your variables' values in the DB, then (again using an ASP/PHP script) call/use them. Not an easy script-writing task, but plenty of examples avaialble in ASP (PHP forums) that can be adapted. Once scripted and stored in a server side DB, then available for re-use, revision. Can hold many data fields and values. HTH

saratogacoach
  • 140
  • 2
  • 9