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.
Related
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 5 years ago.
To be honest, firstly I thought I could easily find answer in Google, but strange, there are very little information about it.
Could anybody explain, what does this code do? What is the advantage (purpose) of using this technique?
(function(document) {
...
}(document));
This is an Immediately-Invoked Function Expression (IIFE) which is basically a function that is declared and called all in one go. It's purpose is to encapsulate logic without muddying up the global namespace (any variables that you declare in that function are only visible in the function). You can find more information here, or just Google IIFE.
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:
What is the purpose of a self executing function in javascript?
(21 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
This probably is not a new question, but where is the purpose of wrapping a function or codes inside ((function () {...})());? for instance,
//Self-evoking anonymous functions
((function () {
alert("hi");
})());
What's the difference with no wrap,
alert("hi");
I still get the same result - hi
What can you pass/ put in the brackets in the end bit - })());? and why?
Using a function creates a scope. You can have params inside and do more than just alerting.
Now you can do the same without a function, but then you will keep the state on the window object and thats something that you would like to prevent in some cases.
This question already has answers here:
javascript function leading bang ! syntax
(7 answers)
Closed 9 years ago.
guys i want to know is there any different between this anonymous :
first :
(function(){
//statements
})();
second :
(function(){
//statements
}());
third :
!function(){
//statmeents
}();
i need a clear explanation about this , thanks all :D
The one and only difference is that the last variation uses fewer bytes.
All three use the language's syntax rules to force the function to be a function expression (which can be immediately invoked) rather than a function declaration (which cannot be invoked, must be named, and is subject to hoisting).
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Location of parenthesis for auto-executing anonymous JavaScript functions?
I'm curious now that I have seen these two similar examples:
(function ($) {
// code
}(jQuery));
and
(function ($) {
// code
})(jQuery);
Is there any difference and if so what?
There's no difference, they do the same thing. You need the parens around the anonymous function, but whether you put the parens triggering the call within those or outside them doesn't matter. Some feel the former is more "correct" (though I've never heard a strong rationale, just Crockford's assertion that it's more clear, which I contest). The latter (in my experience) is much more common.
having a pair of bracket out side the function definition makes no sense, for now, there's no diff.
these 2 equals function(){}(jQuery);