What are the differences between these three types of module pattern?
Asked Answered
C

3

4
1)function () { 
    // code here...

}();

2)(function () { 
    // code here...

})();



3)(function () { 
    // code here...

}());

What are the differences (especially third variant)? Are they all the same?

Colous answered 4/10, 2011 at 11:13 Comment(1)
You can find more information on this at #441239 and #423728Orit
O
2

First one gives a syntax error. Second and third versions define a anonymous function and immediately execute it. Second and third versions are also called Immediately Invoked Function Expressions.

You might also encounter another version which looks like this. This is equal in functionality to 2nd and 3rd version but it just negates the return value.

!function() {
   //some code
}()
Orit answered 4/10, 2011 at 11:18 Comment(2)
No, it does not give a syntax error because of the negation operator. This form just executes the function and negates the return value and returns it to the caller.Orit
You might want to have a look at this link benalman.com/news/2010/11/… ..It covers most of the forms of self invoking functions and their differences.Orit
K
2

2 and 3 are exactly equivalent. There is no functional difference between them.

1 is a syntax error. Because the function is not wrapped in brackets, it is treated as a function declaration. It is invalid because function declaration need to be named. The brackets make it a "function expression"; these do not need to be named.

Kisumu answered 4/10, 2011 at 11:16 Comment(2)
1 is a syntax error for two reasons. Firstly, like you said, a function declaration needs to be named, but also because the trailing () is not valid syntax. It certainly won't execute the function just declared even if it were named...Ablaze
@Ablaze Well, yes, But the parser doesn't even get to that point because of the previous error.Kisumu
O
2

First one gives a syntax error. Second and third versions define a anonymous function and immediately execute it. Second and third versions are also called Immediately Invoked Function Expressions.

You might also encounter another version which looks like this. This is equal in functionality to 2nd and 3rd version but it just negates the return value.

!function() {
   //some code
}()
Orit answered 4/10, 2011 at 11:18 Comment(2)
No, it does not give a syntax error because of the negation operator. This form just executes the function and negates the return value and returns it to the caller.Orit
You might want to have a look at this link benalman.com/news/2010/11/… ..It covers most of the forms of self invoking functions and their differences.Orit
M
2

1st one is not valid, however you can do following instead to make it work:

var myfunction = function () { 
    // code here...
}();

As other answers pointed out that there is no difference between 2nd and third, they are same.

Instead of using parenthesis, following is also valid:

!function() { /*  code here... */ }();
~function() { /*  code here... */ }();
+function() { /* code here... */ }();
-function() { /*  code here... */ }();
new function() { /*  code here... */ };
new function(arguments) { /*  code here... */ }(arg);

Note: People used to call these functions 'Self-Executing Anonymous Function' but the term is incorrect. Now they are called 'Immediately Invoked Function Expressions (IIFE)' pronounced "iffy"!

Maleficence answered 8/3, 2013 at 14:25 Comment(2)
You should not use new, especially if you care about the return value.December
if you use new function(){} this will return a object with constructor property and I can run the function again this is prevent by non new example or IIFE exampleWalden

© 2022 - 2024 — McMap. All rights reserved.