Skip to main content
Roy Marshall
Known Participant
August 11, 2011
Answered

[JS][CS5] question, and comments

  • August 11, 2011
  • 1 reply
  • 869 views

HI All.

I must first start off by recommending anyone seriously wanting to get into Scripting, or (like me) am fairly knowledgable, but needed the resource, to check out "InDesign CS5 Automation Using XML & Javascript" by Grant Gamble. (Amazon, Kindle etc...)

I am sure I am not the only one who has made his way, stumbling and even hacking through code to produce scripts that work well, but not really understanding why certain things were done in a certain way. This books helps a great deal with this, laying a good basis for further development.

Anyway, on to my question. In the afore mentioned book, he talks about making a global variable/object that is used to pass variables into throughout the script ie:

var g = {};

main();

g = null;

function main(){

//code here

//g.win = new Window("dialog",undefined,"New Window");

//etc.....

}

I can see how this works, with "g" being a master item, but why would this be needed? It seems more confusing to me, but maybe cause I am self taught, and not really been shown the "proper" way to use variables!

Can someone explain the reason behind this for me.

Many Thanks

Roy

This topic has been closed for replies.
Correct answer Jongware

The ONLY variable that 's ever "used" in this script is g. All other variables are either local (because defined inside a function) or, when a global one is needed, defined 'inside' g.

By ensuring g is null right before you exit, you prevent polluting the memory and/or data leaks out of the script into the global memory pool.

1 reply

Jongware
Community Expert
JongwareCommunity ExpertCorrect answer
Community Expert
August 11, 2011

The ONLY variable that 's ever "used" in this script is g. All other variables are either local (because defined inside a function) or, when a global one is needed, defined 'inside' g.

By ensuring g is null right before you exit, you prevent polluting the memory and/or data leaks out of the script into the global memory pool.

Roy Marshall
Known Participant
August 11, 2011

Thanks Jongware.

So is this normal practice? 

Can I then use this 'g' variable to store ANY type of variable? Arrays, strings, etc...

Cheers

Roy

Jongware
Community Expert
Community Expert
August 11, 2011

Roy Marshall wrote:

So is this normal practice?   Can I then use this 'g' variable to store ANY type of variable? Arrays, strings, etc...

Normal? It's sort of anally retentive, IMO ...

Can you story anything? Yeah, sure, Javasccipt allows that. But if you are in doubt of its usefulness ... don't use it.

It's perfectly possible to write all of scripts with all of your variables global, and without using a function 'main'. The name of this function suggests a similar usage as in the C programming language, but it could be named 'whatever_you_want' -- it's not part of the language specification.

The same goes for global variables. Use them at will, I'd say, until you run into any side effects (not really likely) or learn to program cleanly (not really necessary, as I can attest ), or learn to be Paranoid and don't want anyone else snooping in the insides of What You Were Doing In That Script.

My conviction is there is nothing wrong when starting to program to use global variables. Only when you learn to use functions, you're going to appreciate the details of global vs. local variables (and then usually after making a number of silly mistakes in this regard).