This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does the exclamation mark do before the function?
I;ve just came across a tablesorter plugin, and while looking at the source, I found that plugin is set up with Self-Invoking Anonymous Function - nothing unusual. Yet, I don't know what is the purpose of leading ! character:
!(function($) {})(jQuery);
I mean, how does it differ from this:
(function($) {})(jQuery);
For all intents and purposes, it doesn't differ at all. It negates the return from the function call, but since the return value isn't assigned to anything then it's pointless.
Related
This question already has answers here:
What is 'Currying'?
(23 answers)
Closed last year.
I'm trying to solve some javascript puzzle. I have some function (for example a function which calculates summa).
But there is a weird calling of that function.
example:
sum(5)
or sum(5,6) - should be 11
or sum(5)(6) - should be 11
or sum(1)(2)(3) - should be 6
Have never seen such function calling sum(1)(2)(3)(4)....
Can you please explain or maybe put into documentation where this stuff is explained?
Thanks a lot.
If the sum function returns a value that it can reuse, you can chain the numbers on and on.
If you can share the function it can be easier to explain
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.
This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 8 years ago.
Is there any difference between the following? Do they all work in the same way?
I've seen certain use-cases for .call() but I've never seen an explanation as to why the function call brackets are either inside or after the anonymous function declaration.
(function() {
}());
^^
(function() {
})();
^^
(function() {
}).call();
The first two are the same, and differ by style only*; the last one is different in that it gives you the ability to control what the value of this will be inside the IIFE. For example
(function(){
this.a = 12;
}).call(foo);
will add the property a to the object foo.
*Of course Douglas Crockford has a preference
The location of the () inside or outside the main () doesn't matter in the slightest. (Much) more discussion in this other question, but that question doesn't address the call option you raised.
call requires at least one argument according to the specification, so to be largely the same as your first two options, you'd want:
(function() {
}).call(undefined);
...to be sure some implementation doesn't get uppity with you for not supplying the argument.
I prefer 2nd way. JSLint uses the first way. You should always use .call() with an argument, so the third variant is wrong.
There is no difference between 1 and 2 though.
This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 9 years ago.
Is there any differences between javascript modules:
(function(){}())
vs
(function(){})()
First from book "good parts" by Crockford.
Second is code generated with Typescript.
There is no different. Also you can write the third option if your function doesn't return any value
!function(){}()
No, there is no difference between those two functions and how they're called. In both cases, you're creating an anonymous function and executing it immediately.
The only reason the "outer" parens are required is that when the JavaScript parser is expecting to see a statement, if it sees function it assumes what follows will be a function declaration. But we want to give a function expression, so by giving it an initial (, we put it into a state where it's expecting an expression.
But where the () to call the function go (after the } or outside the wrapping parens) doesn't make any difference.
No there is no difference, they both work the same. I tend to use the latter... it just seems to make more sense. (function(){}) defines the function and then you call it with (). In either case though, use a (leading)semicolon before the first (. Reference
This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I have a simple question.
I've found this code, and i don't know this statement
!function ($) {
// (...)
}(window.jQuery);
why put ! before a function?
i've found this on bootstrap.js file, and i really want to know.
Thanks!
It is a duplicate as nnnnnn mentioned. What the code is doing is executing the anonymous function while passing window.jQuery as a parameter, which will be referenced as $ inside the function. This allows the use of $ to reference jQuery without conflicting with any other library that might use the dollar sign.
This is a more readable version of the code:
(function($){
// here, $ references jQuery and any variable or function
// declared here cannot be overridden outside of this function
})(window.jQuery)
The ! will always parse the statement as being true if the statement is not able to be parsed.
You can see this by using,
javascript:alert(!function(){}())
Where the resulting response is true