This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
What does (function($) {})(jQuery); mean?
(6 answers)
Closed 8 years ago.
What does (function($) {})(); mean?
Its an Immediately-Invoked Function Expression (IIFE).
It means that the code between the curly braces will be executed as soon as its parsed and inside a closure. This means that any variables declared inside the function body with var will be discarded from memory after the function is finished executing. This is a way to isolate code and prevent namespace polution. You can also use this to rename variables for a particular scope:
For example, consider jquery:
(function($){
//inside the closure, jquery can be accessed using '$'
$(...)
})(jquery);
or
(function(customJqueryName){
//inside the closure, jquery can be accessed using 'customJqueryName'
customJqueryName(...)
})(jquery);
Check out closures: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures
IIFE: http://benalman.com/news/2010/11/immediately-invoked-function-expression/
It's an anonymous inline-function, which will be called just after definition.
Related
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 2 years ago.
I came across with a piece of code with new syntax for me, What this syntax means in js? I mean this parentheses at the beginning:
(function () {
//...
})()
It's called IIFE
basically, you can define a a function and invoke it immediately without declaring a name for it.
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 5 years ago.
Could I get an explanation of the follow code snippet?
(function()
{
alert();
})();
This looks like an anonymous function and the alert() function gets executed. I don't understand the semantic meaning of the outer parentheses. What does this part of the snippet mean?
(
)()
this represents the immediately executable function. in easier way, it means that function being declared and called/executed simultaneously.
This question already has answers here:
Why a Name Function Expression not available outside function body [duplicate]
(3 answers)
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
I'm curious, why not just javascript function scope it? Why just add a parenthesis can not access it? My guess is that the parentheses and javascript related operations, but do not know exactly why this child principle and design?
(function test(){
console.log( test );
})();
test();//Uncaught ReferenceError: test is not defined IE8- is ok
or
(function test(){
console.log( test );
});
test();//Uncaught ReferenceError: test is not defined IE8- is ok
When you wrap a function in parentheses like you did, it does put it in a new scope.
It also acts like a return value, which is why it can be called as an IIFE, or Immediately Invoking Function Expression.
Another way to re-write it, which will make more sense is like so:
var myFunc = (function test(){
alert('Hello!');
});
myFunc(); // Works!
test(); // Doesn't work!
To learn more about this, you should read about IIFE - Immediately-invoked function expression.
The short version - the function runs in its own scope, unless you pass parameters to it, like so:
(function test(x) {
alert(x); //alerts "yey!";
})("yey!");
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.
I see this in some HTML files that use jQuery, at the bottom:
(function() {
setTimeout(function(){window.scrollTo(0,0);},0);
})();
What does it mean to put the whole function in round brackets?
The code you gave as example is a self-executing anonymous function.
You can read more about them here.
Relevant text from that article:
What’s useful here is that JavaScript has function level scoping. All variables and functions defined within the anonymous function aren’t available to the code outside of it, effectively using closure to seal itself from the outside world.
(function() {})(); means it is self executing anonymous function. It get called immediately when java script get rendered. More details you can search it.
setTimeout is a function which accepts two parameters:
First parameter: A function
Second parameter: an integer value in milliseconds.
By calling the setTimeout function, the function from first parameter will be called/invoked after the amount of time specified in the second parameter.
This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Why define an anonymous function and pass it jQuery as the argument?
(5 answers)
Immediately-Invoked Function Expression (IIFE) In JavaScript - Passing jQuery
(1 answer)
Closed 9 years ago.
Could someone explain in layman's terms what the following construct is doing:
(function(jQuery) {
// some javascript code here
})(jQuery);
of course, any "read this post", or "read this link", or even "don't do that! this is a better way" kind of responses are also more than welcome.
This code is creating an anonymous, self-invoking function with a reference to a variable called jQuery from the parent scope passed as first argument.
This syntax is often use to "namespace" the code you are writing to avoid the pollution of the global scope, very popular with JavaScript Module pattern.