Immediately executed anonymous function syntax ambiguity [duplicate] - javascript

This question already has answers here:
Immediate function using JavaScript ES6 arrow functions
(3 answers)
Why are parentheses required around JavaScript IIFE? [duplicate]
(3 answers)
Closed 3 years ago.
Why is the following a syntax error?
(()=>{console.log(1)}())
()=>{console.log(1)} is clearly(?) an expression that evaluates to a fat arrow function, and () "should" call it?
Presumably because it is ambiguous. What is the ambiguity?
Obviously, I realise the following works:
(()=>{console.log(1)})()

Related

How are these methods of writing functions different? [duplicate]

This question already has answers here:
How does this object method definition work without the "function" keyword?
(2 answers)
ES6 Object Literal Syntax for Methods
(1 answer)
Closed 2 years ago.
I have code that has the following functions written:
testing: function() {
///
}
what is the difference of writing it as :
testing() {
///
}
Do they mean the same thing and have the same purpose?

Why arguments is not defined in arrow function in ES6? [duplicate]

This question already has answers here:
Official information on `arguments` in ES6 Arrow functions?
(2 answers)
Do ES6 arrow functions have their own arguments or not? [duplicate]
(4 answers)
Closed 7 years ago.
For this code
var sum = () => {console.log(arguments); ... }
sum(2, 3)
Get error Uncaught ReferenceError: arguments is not defined.
Why arguments is not defined in arrow function?

What (jQuery) means in the end of function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
(10 answers)
Closed 7 years ago.
I have a function:
var waitingDialog = (function($){
.....
return{
}
})(jQuery);
Also could you explain what $ means in the function? Is it going to work without that?
It means that jQuery (if it exists) will be passed to the function. $ is merely the name that variable will take in the scope of the function.
There is a section about that in official jQuery website : https://learn.jquery.com/plugins/basic-plugin-creation/#protecting-the-alias-and-adding-scope

Javascript - what is the purpose of wrapping functions or codes in a function? [duplicate]

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
This probably is not a new question, but where is the purpose of wrapping a function or codes inside ((function () {...})());? for instance,
//Self-evoking anonymous functions
((function () {
alert("hi");
})());
What's the difference with no wrap,
alert("hi");
I still get the same result - hi
What can you pass/ put in the brackets in the end bit - })());? and why?
Using a function creates a scope. You can have params inside and do more than just alerting.
Now you can do the same without a function, but then you will keep the state on the window object and thats something that you would like to prevent in some cases.

what does !function() means in Javascript? [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
I can read and write some JavaScript, but today I came across a .js file starting with
!function() {
/*
code here
*/
}.call(window);
Question
what does ! infront of function means?
It returns "not" (!) of the result of a calling the anonymous function with a "this" of window.
Negating the result of .call(window). Not the function.

Categories

Resources