(function(){})() vs. !function(){}() [duplicate] - javascript

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 9 years ago.
In jQuery javascript code I see
(function(window, undefined) {
})(window);
And in Twitter
!function(window, undefined) {
}(window);
Can someone tell what the difference between these two approaches is?

Using the ! operator before the function causes it to be treated as an expression, so we can call it:
!function() {}()
http://jasonlau.biz/home/faq/what-is-the-exclamation-mark-used-for-in-code

Related

what does this mean? !function() [duplicate]

This question already has answers here:
What does the exclamation mark do before the function?
(8 answers)
Closed 5 years ago.
What does this mean?
!function a(){
}({
x: [function(){alert(2);}]
})
When I reading some codes in a chrome extension, I have found this block of code. I am confused how this should work.
It's just another way of writing an IIFE, using a ! operator instead of parentheses. There are a variety of operators that can be used this way:
! function(text) {
console.log(text);
}('hello');

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.

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

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.

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.

Categories

Resources