Javascript anonymous function call

JavascriptAnonymous Function

Javascript Problem Overview


I was reading JS sources from Twitter — on my way to improve my JS knowledge base, when I came across the strange way of calling anonymous function:

!function( $ ) {
    ...
}( window.jQuery );

... and this works! :)

It's obvious to everyone, that this:

function ( $ ) { ... } ( window.jQuery )

does not work (Syntax error), while this one is correct:

(function ( $ ) { .... })( window.jQuery )

Can anyone please explain this magic (why case with !function works)?

Javascript Solutions


Solution 1 - Javascript

When the keyword function is met in a statement position (as the first token in a statement), the function declaration is expressed as a function statement. Function statements are hoisted to the top of the scope, cannot be immediately invoked and must have a name.

When the keyword is met in an expression position (i.e. not as the first token in a statement, in your example ! is the first token), the function declaration is expressed as a function expression, which may be anonymous and returns the value of the newly created function. Since it returns the value of the newly created function, you may immediately invoke it by adding parenthesis after it.

Wrapping the declaration inside parenthesis does the same but is more common than prefixing it with a ! or +:

(function () {
    ...
})();

Solution 2 - Javascript

The second form function () {} is a statement. The ! operator converts this into an expression. You will also find cases where people use - or + before the function keyword.

When you have an expression evaluating to a function, you can call that function by using the () operator.

Another (perhaps easier to understand) way to achieve the save same effect is with another set of parenthesis:

( function(x) { body; } )(arg);

By placing the function inside the parenthesis, you again convert it to an expression, which evaluates to a function. This function is called with arg as an argument.

As an arrow function:

( (x) => { body; } )(arg);

Solution 3 - Javascript

As far as the exclamation mark goes, that is not magic. It converts the result to a true/false.

Your problem may be that your anonymous function has an error inside it.

Solution 4 - Javascript

this sounds like a javascript syntax bug:

a named function could be a statement however, an anonymous function just consider it as an expression.

Pro: you can do function() { ... }()

Con: you cannot do function() { ... }

But why people would want to define a anonymous function without invoking it? So the con is not really a concern.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionKonstantin LikhterView Question on Stackoverflow
Solution 1 - JavascriptFelix LoetherView Answer on Stackoverflow
Solution 2 - JavascriptnimrodmView Answer on Stackoverflow
Solution 3 - JavascriptShaneView Answer on Stackoverflow
Solution 4 - JavascriptStone ZhongView Answer on Stackoverflow