Forums > Javascript

Globals in MAX javascript – best practice

December 22, 2013 | 3:02 pm

I read from the documentation that it recommends using e.g:
g = new Global("j1");
g.testvar= "value";

However if I put a value at the top in a .js file without the var keyword then the variable is accessible from other js scripts in the same patcher. Is this not a recommended approach?

e.g
//Beginning of some .js file..
tesvar = "globalvalue";


December 22, 2013 | 3:57 pm

Hi !
yes and nope .its accessible within EVERY patcher . not only inside current patcher . its not recommended at all in javascript . also these variables are very slow .

using Global you can share data within your patcher across other JS instances .


December 23, 2013 | 1:23 am

Use of global variables in JavaScript is probably the Number 1 cause of impossible-to-track-down errors. Seriously.

So of course every JavaScript beginner in the world uses globals, thinking "oh, this is so nice and easy and convenient." And the problem is that the little tiny bit of code works OK on its own. But mix that into a just slightly larger project and out of the blue things stop working and you will lose hours trying to track that little sod of a problem down. Make that a larger project with multiple programmers and you will waste days of company time looking for the bugger.


December 23, 2013 | 3:18 am

Ok – thanks for the input!


Viewing 4 posts - 1 through 4 (of 4 total)