Confusion about anonymous function declaration in javascript [duplicate] - javascript

This question already has answers here:
javascript function leading bang ! syntax
(7 answers)
Closed 9 years ago.
guys i want to know is there any different between this anonymous :
first :
(function(){
//statements
})();
second :
(function(){
//statements
}());
third :
!function(){
//statmeents
}();
i need a clear explanation about this , thanks all :D

The one and only difference is that the last variation uses fewer bytes.
All three use the language's syntax rules to force the function to be a function expression (which can be immediately invoked) rather than a function declaration (which cannot be invoked, must be named, and is subject to hoisting).

Related

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.

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.

what does !function in Javascript mean? [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 5 years ago.
Sorry for posting this but !function is not google-able and I did not find it in my JavaScript code.
Here is how Twitter uses it:
Tweet
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
from https://twitter.com/about/resources/buttons#
It is short-hand or alternative of self-invoking anonymous function:
(function(){
// code
})();
Can be written:
!function(){
// code
}();
You can also use + instead of !.
If you simply did:
function(){
// code
}();
That will create problems, that's why you need to add ! before it which turns the function declaration into function expression.
Quoting docs, section 12.4:
An ExpressionStatement cannot start with the function keyword because
that might make it ambiguous with a FunctionDeclaration.
To better understand the concept, you should check out:
Function Declarations vs. Function Expressions
they're negating the result, not the function itself:
!function( x ){ return x }( true );
!true
false
In reality, it's a slightly compressed form of:
(function(){}())
since it requires 1 less character. The reason it's needed is that you can't call a function declaration directly, e.g. this is invalid:
function(){}()
but adding the ! at the beginning turns it into a function expression and makes it work.
It's usually used to work around a quirk in the JavaScript syntax. This gives a syntax error:
function() {
}();
It's read as a function declaration (like function foo () {}), rather than a function expression. So adding the ! before it, or wrapping it in parentheses, forces the parser to understand that it's an expression.

What's the difference between these two seemingly similar closure types? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Location of parenthesis for auto-executing anonymous JavaScript functions?
I'm curious now that I have seen these two similar examples:
(function ($) {
// code
}(jQuery));
and
(function ($) {
// code
})(jQuery);
Is there any difference and if so what?
There's no difference, they do the same thing. You need the parens around the anonymous function, but whether you put the parens triggering the call within those or outside them doesn't matter. Some feel the former is more "correct" (though I've never heard a strong rationale, just Crockford's assertion that it's more clear, which I contest). The latter (in my experience) is much more common.
having a pair of bracket out side the function definition makes no sense, for now, there's no diff.
these 2 equals function(){}(jQuery);

Categories

Resources