Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
-2
rated 0 times [  3] [ 5]  / answers: 1 / hits: 15668  / 12 Years ago, wed, april 25, 2012, 12:00:00

In this thread I found a JavaScript code snippet which I want to use.



The code looks like:



(function(global) {
// the function code comes here
})(this);


How can i call this function to execute the code? What do I have to pass in for this global variable?


More From » function

 Answers
11

The function is immediately executed, you do not execute it by calling it.



It is a function literal definition, followed by two parens which causes that function to invoke immediately. Read more: Immediately-Invoked Function Expression (IIFE)



Whatever code you place inside is run right away. Anything placed in the invocation parens is passed into the function as an argument. Assuming your sample code was defined in the global scope, this is the window object, and is referenced as global within the function body. It is a great way to encapsulate your programs to avoid variable collision, force strict mode, and much more.


[#85988] Tuesday, April 24, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
makenatiffanic

Total Points: 412
Total Questions: 106
Total Answers: 90

Location: Marshall Islands
Member since Tue, Sep 21, 2021
3 Years ago
;