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.
Related
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.
This question already has answers here:
Explain the encapsulated anonymous function syntax
(10 answers)
Closed 6 years ago.
Lets assume we have
function () {}.bind(null);
and
(function () {}).bind(null);
Why the first one doesn't work in developer console (it throws syntax error) but both of them work in code (".js" file in website).
I think its because you are trying to define an anonymous function. Console has no future way of referencing it so its a pointless endeavour, youre defining a function you have no way of calling. If you define it as a variable it works:
var a = function () {}.bind(null);
Also I dont think its binding related. Defining an anonymous function without binding and you have the same error
Edits
A statement that begins with the keyword "function" must be a valid
function declaration statement. That requires a name for the function.
In an expression (or expression statement), that rule is different; no
name is necessary because the function acts as a value in that
context. No name is required then.
via #Pointy from this answer
A function declaration cannot be anonymous, but a function expression
can. A stand alone anonymous function looks like a function
declaration that is missing an identifier to JavaScript. But, combined
with an operator, JavaScript treats an anonymous function as the
operator's operand expression.
via #gilly3 from this answer
This question already has an answer here:
Using function.prototype.bind directly on function declaration
(1 answer)
Closed 7 years ago.
I have been programming in JS for about a year, and I swear there was a way to do this:
function a(){
}.bind(this);
or
function a(){
}.apply(this,null);
I can do this:
(function a(){
console.log('b');
}).apply(null);
a(); //but this will throw an error
is there a way to do what I am trying to do? I just want to invoke apply, call or bind on a function without losing scope.
Are you looking for
var a = function a() {
// ...
}.bind(this);
? You can't use a function as an expression in the context of a function declaration statement; the syntax just doesn't allow it. However, you can get an effect very similar to a function declaration statement by instantiating the function in an expression and assigning the reference to a local variable, as above. When you do that, your function is instantiated in the context of an expression, and so using it as an object base to call .bind() works fine.
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.
This question already has answers here:
Immediate function invocation syntax
(3 answers)
Closed 9 years ago.
In order to use the $ symbol in jquery and not have to use jQuery.functionname, we use this
(function($) {
})(jQuery);
(In drupal, you actually have to specify this implicitly).
I don't understand this javascript syntax, why is there an initial parentheses? How is the (jQuery) at the end used?
It's just an anonymous function with an argument that's automatically invoked.
For example, if we were to expand it out a bit you'd end up with something like this:
var anon = function($) {
...
};
anon(jQuery);
The $ is a valid identifer in JavaScript and we pass in the existing jQuery object into the function for use through $, as it could be replaced later.
All that's doing is declaring an anonymous function and executing it immediately, passing in one argument (jQuery) into the function. That argument is given the name $ which can be used throughout the scope of the function.
The brackets around the function aren't strictly necessary in all contexts; see the comment under this answer for details. The gist is that they're needed here to make the function behave like an expression instead of a statement (function declaration).