What is the significance of and reason for wrapping the entire content of a JavaScript source file in a function block?? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the significance of and reason for wrapping the entire content of a JavaScript source file in a function block??

21st Jan 2017, 4:27 AM
Susan Mizan
Susan Mizan - avatar
2 Answers
+ 4
So that you can recall it? wut?
21st Jan 2017, 4:47 AM
David Sebastian Keshvi Illiakis
David Sebastian Keshvi Illiakis - avatar
+ 2
To avoid conflict problems when you run different scripts ( as if both use same name variable for different purpose ), and also limit the use of 'globals' variables... You declare an anonymized fonction as this: (function(args) { })(param); the declaration is placed in between parenthesis, and is executed with paremeters in second parenthesis pair, as soon as declarated... Another reason for wrapping part of ( or entire ) code in function, is in case to need to delay execution ( typically for waiting the DOM be ready ) with declaring the function as soon as it's assigned to an event listener ^^
21st Jan 2017, 8:26 AM
visph
visph - avatar