Whats ist the difference between these two javaScript syntaxes? [duplicate] - javascript

This question already has answers here:
Location of parenthesis for auto-executing anonymous JavaScript functions?
(4 answers)
Closed 7 years ago.
Both of them works without errors, is there any difference?
(function(){}());
(function (){})();
here are some working examples:
console.log(
(function (a) {return a*2;}(3))
);
console.log(
(function (a) {return a*2;})(3)
);

I guess both are same, only thing is one is wrapped inside parantheses () and the other is not.
(function(){}());
And this is creating and executing it immediately:
(function (){})();
Example for the second case would be:
function sayHello () {
alert("Hello");
}
sayHello ();
The above is equivalent:
(function () {
alert("Hello");
})();
Both are the same, it is just the preference of coding style.

Related

Can I invoke functional literal on creation with ()? [duplicate]

This question already has answers here:
Can I name a JavaScript function and execute it immediately?
(9 answers)
Closed 5 years ago.
I thought this would work but it does not. Is it possible to invoke a functional literal on creation like this and have it still be available for use later?
var myFunction = function() {
alert('Hi!');
}();
If you want your function to return something meaningful you should split function creation and invocation.
If not, you could do it this way
var myFunction = function me() {
console.log('Hi!');
return me
}(); // first run
myFunction() // second run
Easiest would be:
(myFunction = function() {
alert('Hi!');
})();
myFunction();
Note that you may declare myFunction before it, it is considered bad style not doing so.

;(function() {})(); in JavaScript [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Can someone explain this JavaScript auto executing function?
(6 answers)
Closed 6 years ago.
I am looking at a JavaScript file, and it is formatted as follows:
;(function() {
// functions and stuff
})();
What does this mean, and what is this 'technique' called?
Immediately-invoked function expression
Its called Immediately-Invoked Function Expressions (IIFEs) like
// variant 1
(function () {
alert('Woohoo!');
})();
// variant 2
(function () {
alert('Woohoo!');
}());
You can read more about it here
This is an anonymous self-executing function.
I would refer you to these resources as of why:
https://en.wikibooks.org/wiki/JavaScript/Anonymous_functions
http://esbueno.noahstokes.com/post/77292606977/self-executing-anonymous-functions-or-how-to-write

Javascript ( function () { ... } ) (); [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
Hello I've got a function like this one.
function doSomething () {
( function () {
// callbacks and code
}
)();
}
I am not use to that typing, I'm looking forward to understand how is interpreted by the browser or what does it mean the ( function () ) ();
Thought I know the differences between calls and assigns , I can't read this properly , surely is something simple but I don't really get it.
Thanks.
Is a self invoked anonymous function:
A self-invoking anonymous runs automatically/immediately when you create it and has no name, hence called anonymous. Here is the format of self-executing anonymous function:
(function(){
// some codeā€¦
})();
https://sarfraznawaz.wordpress.com/2012/01/26/javascript-self-invoking-functions/

Why javascript function parentheses can not access the outside? [duplicate]

This question already has answers here:
Why a Name Function Expression not available outside function body [duplicate]
(3 answers)
How do JavaScript closures work?
(86 answers)
Closed 7 years ago.
I'm curious, why not just javascript function scope it? Why just add a parenthesis can not access it? My guess is that the parentheses and javascript related operations, but do not know exactly why this child principle and design?
(function test(){
console.log( test );
})();
test();//Uncaught ReferenceError: test is not defined IE8- is ok
or
(function test(){
console.log( test );
});
test();//Uncaught ReferenceError: test is not defined IE8- is ok
When you wrap a function in parentheses like you did, it does put it in a new scope.
It also acts like a return value, which is why it can be called as an IIFE, or Immediately Invoking Function Expression.
Another way to re-write it, which will make more sense is like so:
var myFunc = (function test(){
alert('Hello!');
});
myFunc(); // Works!
test(); // Doesn't work!
To learn more about this, you should read about IIFE - Immediately-invoked function expression.
The short version - the function runs in its own scope, unless you pass parameters to it, like so:
(function test(x) {
alert(x); //alerts "yey!";
})("yey!");

why do i need to just '()'" at the end of JavaScript function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 9 years ago.
I'm trying to understand the difference between :
var x = function () { ....}
(function () { ....} ) ();
I get it that the first function will put the results on x.
that and when exactly the second one will be fired? and why do i need the (); at the end?
This is an example of the Immediately-invoked function expression.
The function is executed immediately because () is how JavaScript calls functions. The syntax might be confuse you, because the function does not have a name, but ( function(){} )() just immediately calls the function with no arguments.

Categories