Copy link to clipboard
Copied
The code shown below is from page 70 of Grant Gamble's InDesign JS book. The author states that main() function is called that will execute all of the code. Will someone please explain the reasoning for this? What is the purpose of the main() function?
var g = {};
main();
g = null;
function main(){
blnResult = createDialog();
//if(blnResult){addWatermark()};}
main is the name of a function, a function can be thought of as a collection of statements that are clubbed together under a name using which we can reference these statements and execute them. A function may take an input and produces an output.
In JS the code is read from top to bottom and executed, so in the code snippet you shared the author has defined a method called main. The definition of a function starts with the keyword function followed by the function name and an argument list which
...Copy link to clipboard
Copied
main is the name of a function, a function can be thought of as a collection of statements that are clubbed together under a name using which we can reference these statements and execute them. A function may take an input and produces an output.
In JS the code is read from top to bottom and executed, so in the code snippet you shared the author has defined a method called main. The definition of a function starts with the keyword function followed by the function name and an argument list which is enclosed in () and separated by a comma. So your code is executed in a top bottom fashion like below
var g = {}; //Creates an object
main(); //Main method is called, i.e. the statements defined in the main function are executed
g = null; //the object is set to null
//This is the definition of the main function,
function main(){
blnResult = createDialog(); //Method names createDialog is called here, and its output value is stored in blnResult
//if(blnResult){addWatermark()};}
Look at the comments i put in your code to explain each statement. main is not a special method you can name it anything you like, just the rule that the code is executed top to bottom should be remembered. Also if you write all the statements of the main method outside then also the code will execute the same like below
var g = {}; //Creates an object
blnResult = createDialog();
//if(blnResult){addWatermark()};
g = null; //the object is set to null
Hope this helps
-Manan
Copy link to clipboard
Copied
Very helpful! Thank you, Manan, for the excellent detailed explanation.
Copy link to clipboard
Copied
In JavaScript (or ExtendScript, Adobe's flavor of it) a main() function is not necessary. There is no harm in having one, but there's no need to. As for why so many scripts have a main() function, I can only guess it comes from programmers of C/C++ (and other similar languages) where main() is required, as it is the default entry point for programs in those languages.
Personally I don't use a main() function but I do like to wrap scripts completely in an immediately executing function, just to make the variable scope private. Also not necessary, but one of those things that stem from habits learned programming JavaScript in other situations, like web apps or server-side Node.
Copy link to clipboard
Copied
Very helpful comment. Thank you, williamc3112933.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more