This question already has answers here:
(...()) vs. (...)() in javascript closures [duplicate]
(3 answers)
Closed 8 years ago.
What is the exact difference between these two lines of code.
var functionOne = (function(){})();
and
var functionTwo = (function(){}());
I've noticed that both were being used while looking through js Module pattern, but i'd like to know what is exact difference between them.
Both are the same Immediately-Invoked Function.
There few different syntax variations. As Douglas Crockford’s JSLint offers the right declaration for self-invoking functions is:
(function () {
//body
}());
Alternative syntax is, which Crockford calls “dog balls”…:
(function () {
//body
})();
Related
This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 years ago.
I often use the closure syntax
var something = (function () {
//TODO: do something
}());
and, I often find people use this syntax
var something = (function () {
//TODO: do something
})();
If both the two behaves the same way, what are the differences between the two?
There's no real difference. Both statements contain function expressions that evaluate to functions that are immediately executed.
This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 years ago.
(function f() {
// do something
}());
vs
(function g() {
// do something
})();
Note the position of the ending parentheses.
Do these execute in the same manner?
Do they restrict the scope of variables differently?
They are the same. Just two notations to do the same thing.
Some people find one notation more intuitive than the other. Just a matter of preference, nothing more than that.
This question already has answers here:
JavaScript plus sign in front of function expression
(4 answers)
Closed 9 years ago.
I found a strange behaviour in Javascript:
function() {
return 10;
}();
This construction doesn't work on all browser because it has a syntax error. But this construction works (returns ten):
+function() {
return 10;
}();
Why?
The + lets the js engine make the difference between this function expression and a function definition.
For more readability, we usually use
(function() {
return 10;
})();
See related article
This question already has answers here:
var functionName = function() {} vs function functionName() {}
(41 answers)
Are named functions preferred over anonymous functions in JavaScript? [duplicate]
(4 answers)
Closed 9 years ago.
I saw the following JavaScript functions works exactly same, Then what is the difference between them other than syntax.
The function are:
var functionName=function(){
//some code here
};
function functionName(){
//some code here
}
I call them in the same way as:
functionName();
Please dont' tell me there syntax is different, Other than that is there any difference like
1)speed of execution
2)Memory utilization etc.
Thanks in advance!
This has been answered many times in StackOverflow. It is just the way of naming. So taking up some points from the answers, I would say:
Function declarations and variable declarations are always moved ("hoisted") invisibly to the top of their containing scope by the JavaScript interpreter. Function parameters and language-defined names are, obviously, already there.
Advantages & Disadvantages:
There are few advantages to naming functions:
names for meta analysis. functionInstance.name will show you the name.
Far more importantly, the name will be printed in stack traces.
names also help write self documenting or literate code.
There is a single disadvantage to named functions expressions
IE has memory leaks for NFE
Another main difference
The difference is that functionTwo is defined at parse-time for a script block, whereas functionOne is defined at run-time. For example:
<script>
// Error
functionOne();
var functionOne = function() {
}
</script>
<script>
// No error
functionTwo();
function functionTwo() {
}
</script>
References
var functionName = function() {} vs function functionName() {}
Are named functions or anonymous functions preferred in JavaScript?
Named function expressions demystified
Function Declarations vs. Function Expressions.
var functionName = function() {} vs function functionName() {}
1st one is Named Function Expressions, which should return some value to the caller.
2nd one is just a function, it's upto you whether you return value or not
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
javascript function vs. ( function() { … } ());
I'm seeing this pattern in several of TodoMVC's JS source:
(function() {
// ...
// ...
}());
What's the specific meaning of this pattern? Note that it is not the self-invoking function which is (function() {})();
What's the specific meaning of this pattern? Note that it is not the self-invoking function which is (function() {})();
You're incorrect, it is an Immediately Invoked Function Expression (IIFE). The parenthesis are just in a different place, but they bind the exact same way.
People often do it in the way you've described to get it to validate JSLint.
It's used for scoping, as JavaScript only has function and global scope (ignoring let).