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.
Related
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:
What is the purpose of a self executing function in javascript?
(21 answers)
Closed 8 years ago.
I cannot think of the term that JavaScript developers use for this kind of practice.
var someSome = (function () { ... } ());
-- Wrapping function within brackets. I vaguely remember someone was calling this fiif? fiff?
And there were many advantages and it was recommended java scripting practice.
Anyone got clue what I am talking about and why is it a good practice?
it might have been even without assignment like below
(function () { ... } ());
Self executing function. Often used as a wrapper around a function block to immediately invoke it, as well as give it closure.
This question already has answers here:
Why use named function expressions?
(5 answers)
Closed 8 years ago.
I looked at the jsf.js file of Mojarra 2.2.8 and saw them using the module pattern. Something like this:
name.space = function() {
var utilFunction = function utilFunction() {
// some implementation
};
return {
exposedFunction: function exposedFunction() {
// using utilFunction
}
};
}();
Is there any benefit of giving the functions a name? As opposed to use anonymous functions. They are bound to either a variable or a property of the same name anyway.
Is this some kind of best practice? Does it maybe improve debugging?
I'm just asking, because I usually see the module pattern used with anonymous functions, and was now wondering.
I think it is justified only when using anonymous functions for obvious reading, for example:
async.waterfall[
function makeOne () {},
function makeTwo () {},
];
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:
(...()) 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
})();