Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

JS Main Function

Participant ,
Feb 19, 2019 Feb 19, 2019

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()};}

TOPICS
Scripting
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 19, 2019 Feb 19, 2019

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

...
Translate
Community Expert ,
Feb 19, 2019 Feb 19, 2019

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 20, 2019 Feb 20, 2019

Very helpful! Thank you, Manan, for the excellent detailed explanation.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Feb 21, 2019 Feb 21, 2019

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.

William Campbell
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 21, 2019 Feb 21, 2019
LATEST

Very helpful comment. Thank you, williamc3112933.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines