This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
Hello I've got a function like this one.
function doSomething () {
( function () {
// callbacks and code
}
)();
}
I am not use to that typing, I'm looking forward to understand how is interpreted by the browser or what does it mean the ( function () ) ();
Thought I know the differences between calls and assigns , I can't read this properly , surely is something simple but I don't really get it.
Thanks.
Is a self invoked anonymous function:
A self-invoking anonymous runs automatically/immediately when you create it and has no name, hence called anonymous. Here is the format of self-executing anonymous function:
(function(){
// some codeā¦
})();
https://sarfraznawaz.wordpress.com/2012/01/26/javascript-self-invoking-functions/
Related
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Can someone explain this JavaScript auto executing function?
(6 answers)
Closed 6 years ago.
I am looking at a JavaScript file, and it is formatted as follows:
;(function() {
// functions and stuff
})();
What does this mean, and what is this 'technique' called?
Immediately-invoked function expression
Its called Immediately-Invoked Function Expressions (IIFEs) like
// variant 1
(function () {
alert('Woohoo!');
})();
// variant 2
(function () {
alert('Woohoo!');
}());
You can read more about it here
This is an anonymous self-executing function.
I would refer you to these resources as of why:
https://en.wikibooks.org/wiki/JavaScript/Anonymous_functions
http://esbueno.noahstokes.com/post/77292606977/self-executing-anonymous-functions-or-how-to-write
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 6 years ago.
I've a code:
var myFn = function(){
//some code
}
myFn();
So I have to define a function, then run it in two rows.
Is there a way to define a function (with storage it in variable) and run it instantly, in one expression? Just a short way of this.
var myFn = function(){
console.log("test");
}()
myFn will be undefined because the function doesn't return anything
This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 years ago.
Both of them works without errors, is there any difference?
(function(){}());
(function (){})();
here are some working examples:
console.log(
(function (a) {return a*2;}(3))
);
console.log(
(function (a) {return a*2;})(3)
);
I guess both are same, only thing is one is wrapped inside parantheses () and the other is not.
(function(){}());
And this is creating and executing it immediately:
(function (){})();
Example for the second case would be:
function sayHello () {
alert("Hello");
}
sayHello ();
The above is equivalent:
(function () {
alert("Hello");
})();
Both are the same, it is just the preference of coding style.
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 9 years ago.
I'm trying to understand the difference between :
var x = function () { ....}
(function () { ....} ) ();
I get it that the first function will put the results on x.
that and when exactly the second one will be fired? and why do i need the (); at the end?
This is an example of the Immediately-invoked function expression.
The function is executed immediately because () is how JavaScript calls functions. The syntax might be confuse you, because the function does not have a name, but ( function(){} )() just immediately calls the function with no arguments.
This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Explain JavaScript's encapsulated anonymous function syntax
I have just read a javascript book but I have seen this code:
1(function() {
// code
})();
what is this ? is a special function ?
As written, it has a syntax error.
I'm guessing it was more like:
(function() {
// code
})();
or
(function() {
// code
}
)();
Break it down:
(FOO)() // calls FOO with no arguments.
And
function() { //creates a function that takes no arguments.
// code
}
Hence together it would create a function that takes no arguments, and then call it. I can't see why you would apart from just showing that you can.
It looks like the intent was to declare the function inline/anonymous and immediately execute it.