What does the following code mean in JavaScript [duplicate] - javascript

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.

Related

What does "+" means in +function($)? [duplicate]

This question already has answers here:
JavaScript plus sign in front of function expression
(4 answers)
Closed 5 years ago.
I had look at this question to know about what this means.
(function($) {
})(jQuery);
I am looking at different bootstrap plugins, which have +function ($), while defining the function.
What does + does here, does it appends this function to other functions ?
To guide the javascript parser, that the thing written near the unary operator + is an expression.
EDIT: You can create javascript functions anonymously. This is one of the syntaxes to create the same. By doing so, when they are called (i.e evaluated), they act like a returning a function value. You can read more from the second link which provides a good description of it.
This link explains it well
Once declared, if not named, these can be executed inline like IIFE (Immediately invoked function expressions) as well. And in this form, they can then be used to create plugins or used as namespaces and attached to window object or jquery object for use later.
Good sample file to see anonymous function code in action
http://www.programering.com/a/MTMwITMwATk.html
It's a bang function
the + operator is faster than usual !
see more at
javascript function leading bang ! syntax

What do these encapsulating parentheses mean in JavaScript? [duplicate]

This question already has answers here:
Explain the encapsulated anonymous function syntax
(10 answers)
Closed 5 years ago.
Sorry about this being so vague, but I'm sure how else to ask this, simply because I don't know what it is. I've seen this once before, but I can't remember what it's doing or where to look for it.
What is this code doing with the outer parentheses and "window" thing?
(function(angular){
//some code
})(window.angular);
I really am sorry. After I get reference to some official documentation or something, I'll delete the post.
This is an example of IIFE (Immediately Invoked Function Expressions). Take a look at here for more explanation.
The first part
(function(angular){
//some code
})
is an anonymous function (it does not have a name). Since you want to execute it immediately (when the page/DOM is loaded), you just call it like any other function
(window.angular);
..with a parenthesis, arguments and a semicolon.
Your argument (window.angular) is just a global object (which is why it is defined on window scope).
In short, you are executing that body of function with an argument, which is defined globally.

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

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.

Is the underscore.js IIFE syntax valid? [duplicate]

This question already has answers here:
Reason behind this self invoking anonymous function variant
(5 answers)
Closed 8 years ago.
I just took a look at the underscore.js source code and when i strip the source code down to its bare containing IIFE it looks like this:
(function() {
}.call(this));
I always used the syntax with outer parantheses (function() {}).call(this); and wondered if this syntax is also valid and common?
If you're asking about the location of the outer ) specifically, then whether it's located immediately after the closing brace or after the entire expression doesn't matter for the most part. Either way doesn't make a difference to how the IIFE is executed.
The only difference here is the .call(this), which is invoked as a member of the function expression — a typical IIFE has just the inner parentheses immediately following the closing brace. The reason .call(this) is used is detailed in a number of other answers including this one.

The usage of (function () { //code })() [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What do parentheses surrounding a JavaScript object/function/class declaration mean?
I simply don't understand the usage of...
(function () {
//code
})()
...thing.
I call that a 'thing' couse' I dont even know its name...
Is this a shorthand for onReady or onLoad event or it is for some kind of scope or closure thing?
If can anyone explain the usage and purpose of this syntax would be appreciated.
It's known as a self executing function - It calls itself after its declarations.
Commonly used not to pollute the global namespace.
For a short but interesting article have a read here:
How Self Executing Functions Work
Is this a shorthand for onReady or onLoad event or it is for some kind
of scope or closure thing?
It is self-invoking anonymous function.
It invokes itself due to () at the end because that's how you normally invoke a function:
someFunc();
It is anonymous because it has no name.
The whole function body is wrapped in () to create local scope of variables inside it. Any variable/function declared in that way won't be available outside (so that global scope is not polluted) unless exposed explicitly.
You can learn more about it here.

Categories

Resources