In javascript, when would you want to use this:
(function(){
//Bunch of code...
})();
over this:
//Bunch of code...
In javascript, when would you want to use this:
(function(){
//Bunch of code...
})();
over this:
//Bunch of code...
Its all about variable scoping. Variables declared in the self executing function are, by default, only available to code within the self executing function. This allows code to be written without concern of how variables are named in other blocks of javascript code.
For example:
This will first alert "3" and then throw an error on the next alert because foo is not defined.
One difference is that the variables that you declare in the function are local, so they goes away when you exit the function and the don't conflict with other variables in other code.
Self executing function are used to manage the scope of a Variable.
The scope of a variable is the region of your program in which it is defined.
A global variable has global scope; it is defined everywhere in your JavaScript code and can be accessed from anywhere within the script, even in your functions. On the other hand, variables declared within a function are defined only within the body of the function. They are local variables, have local scope and can only be accessed within that function. Function parameters also count as local variables and are defined only within the body of the function.
As shown below, you can access the globalvariable variable inside your function and also note that within the body of a function, a local variable takes precedence over a global variable with the same name.
So basically a self executing function allows code to be written without concern of how variables are named in other blocks of javascript code.
It looks like this question has been answered all ready, but I'll post my input anyway.
I know when I like to use self-executing functions.
The function allows me to use some extra code to define the childObjects attributes and properties for cleaner code, such as setting commonly used variables or executing mathematic equations; Oh! or error checking. as opposed to being limited to nested object instantiation syntax of...
Coding in general has a lot of obscure ways of doing a lot of the same things, making you wonder, "Why bother?" But new situations keep popping up where you can no longer rely on basic/core principals alone.
Here's a solid example of how a self invoking anonymous function could be useful.
Output:
10, 10, 10, 10, 10...
Output:
0, 1, 2, 3, 4...
I've read all answers, something very important is missing here, I'll KISS. There are 2 main reasons, why I need Self-Executing Anonymous Functions, or better said "Immediately-Invoked Function Expression (IIFE)":
The first one has been explained very well. For the second one, please study following example:
Attention 1: We are not assigning a function to
MyClosureObject
, further more the result of invoking that function. Be aware of()
in the last line.Attention 2: What do you additionally have to know about functions in Javascript is that the inner functions get access to the parameters and variables of the functions, they are defined within.
Let us try some experiments:
I can get
MyName
usinggetMyName
and it works:The following ingenuous approach would not work:
But I can set an another name and get the expected result:
Edit: In the example above
MyClosureObject
is designed to be used without thenew
prefix, therefore by convention it should not be capitalized.