what does (function () { ... })(); mean? [duplicate] - javascript

This question already has answers here:
What does this "(function(){});", a function inside brackets, mean in javascript? [duplicate]
(5 answers)
Closed 9 years ago.
I see that in many modules for node.js and also for browser, they use to have all their code inside something like this:
(function () {
moduleName.prototype.variable = 'whatever';
})();
Can anyone please explain what this all is, or any links that explain? I have no idea how to search it! Thanks in advance.

This is a pattern called Immediately Invoked Function Expression.
It's mostly useful for isolation purposes.

Related

parentheses before function syntax js meaning [duplicate]

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.

Enclosing parentheses in JavaScript [duplicate]

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.

syntax for the module pattern in Javascript [duplicate]

This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 6 years ago.
I am learning module pattern in Javascript, and have come across these two ways to create a module:
var Module = (function () {
// code
})();
var Module = (function () {
// code
}());
is there a significant difference in these two approaches? if not, which is considered the better practice? Thanks.
Both are the same. The outer round braces are forcing the inside code to be evaluated as an expression. This means that in both cases the function code is treated as function-expression as well. And then this function is immediately executed due to () braces.
So, it should be exactly the same from the point of view of JS interpreter todo list: 1) get function expression, 2) execute it right away.
It only differs from aesthetic point of view - which way it looks more natural for you.

JavaScript function evaluating itself and calling itself at the same time [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 7 years ago.
I am learning about JavaScript functions on MDN
I came across an example that looks similar to this:
var saySomething = ( function(){console.log("hello")} )();
I have also seen this in the jQuery source.
I have not found any explanation of this style of function calling/definition on the MDN function reference.
I know from running the code that it calls itself immediately upon interpretation by the JavaScript engine.
Is this the Grouping Operator in action? Where it says:
First evaluate the body of this function and return it
Since the parentheses () immediately follow it it gets called ?
Google "Immediately Invoked Function Expression" or "IIFE".
The general syntax looks like this:
(function(){
// do something here
})():
Sometimes you'll see arguments passed in as well. It's basically used to wrap your code so none of your variables leak out into the global namespace.

setInterval and setTimeout don't seem to work with a console.log [duplicate]

This question already has answers here:
setTimeout ignores timeout? (Fires immediately) [duplicate]
(3 answers)
Closed 8 years ago.
I am answering my own question in the hopes it will solve someone else's headache:
I could not get the following code to work:
function givePosition(){
var place = $('#two').position();
console.log(place);
}
window.setInterval(givePosition(), 50);
The solution to my problem was to give the function name as a parameter without parentheses, like so:
window.setInterval(functionName, 50);
instead of:
window.setInterval(functionName(), 50)

Categories