What is `(function(document) { ... }(document));` [duplicate] - javascript

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.

Related

Javascript onMouseMove event syntax [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 3 years ago.
I am working on some JavaScript code and I run into this syntax that I have never seen and I am trying hard to understand but cannot find good examples.
Can someone please describe what might be going on here?
function onMouseMove(event) {
(function(ev) {
// some piece of code
})(event);
}
This syntax is used to create an inner scope using a function and that function is immediately invoked with the event object.

What's the difference between these two IIFE invocation syntaxes? [duplicate]

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.

What does the following code mean in JavaScript [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
What does the following code mean in JavaScript :
(function() {
})();
It is a Self-Invoking Anonymous Function.
A self-invoking anonymous runs automatically/immediately when you create it and has no name, hence called anonymous.
More info
Thats a singleton/IIFE (immediately invoked function expression).
Using an IIFE can be helpful when wanting to use a local scope which eliminates binding to global objects like the window.
There is also a slight performance benefit to this approach as you can pass in commonly used objects to the anonymous function. JavaScript first looks for a property in its local scope then works up the chain.

How is this working !function(){console.log("hi")}() [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I read this on the javascript garden site. Can someone explain how it works?
!function(){console.log("hi")}()
The 'executing' parens at the end can't be done legally after a function expression. A typical (more sensical, IMO) way of writing this is with more parentheses:
(function(){console.log('hi')})()
By prepending the ! before the function expression, the JS interpreter reads the function and then runs it. This is because of the precedence of the ! operator vs. calling a function with the final ()
Look at this answer
tl;dr it defines a function to print out 'hi' and immediately calls it.

(function(){})() Is there any documentation of this? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How does an anonymous function in JavaScript work?
What does this JavaScript snippet mean?
I use in my scripts:
(function(){
...[code]..
})()
I can't find documentation about this, i have see scripts when this form take args.
(function(){
...[code]..
})(arg1,arg2)
Somebody have a link or a good explanation about this javascript function?
this is just regular javascript.
you instanciate an anonymous function, and then call it with 2 arguments.
The confusing part I think is the on-the-fly aspect of the operation.
You could have done (at higher cost):
var hnd = function() {...};
hnd(arg1,arg2);
It's known as a Self-Executing Anonymous Function.
Here is the first Google result, which gives a solid overview.

Categories

Resources