SoFunction
Updated on 2025-04-06

Deeply understand self-executed anonymous functions in Javascript

Format:

(function(){
//Code})(); 

Explanation: This is quite elegant code (if you see it for the first time, you might be confused :)), the first pair of brackets surrounding the function (function(){}) returns an unnamed function to the script, and then a pair of empty brackets immediately executes the returned unnamed function, with parameters of the anonymous function in the brackets.

Here is an example with parameters:

(function(arg){
alert(arg+100);
})(20);
// This example returns 120.

Let's take a look at jquery plugin writing

(function($) {
 // Code goes here
})(jQuery); 

This code is equivalent to

var a=functon($)
{//code
};

a(jQuery);

The above article in-depth understanding of the self-executing anonymous functions in Javascript is all the content I have shared with you. I hope you can give you a reference and I hope you can support me more.