Home  >  Article  >  Web Front-end  >  In-depth understanding of self-executing anonymous functions in Javascript (graphic tutorial)

In-depth understanding of self-executing anonymous functions in Javascript (graphic tutorial)

亚连
亚连Original
2018-05-21 11:04:221426browse

Below I will bring you an in-depth understanding of self-executing anonymous functions in Javascript. Let me share it with you now and give it as a reference for everyone.

Format:

(function(){
//代码
})();

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

Let’s take an example with parameters:

(function(arg){
alert(arg+100);
})(20);
// 这个例子返回120。

Come back and take a look at jquery plug-in writing

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

This code is equivalent to

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

a(jQuery);

The above is what I compiled Everyone, I hope it will be helpful to everyone in the future.

Related articles:

Sharing examples of E-mail address format verification in JavaScript

Javascriptnew( )Detailed explanation

Some frequently error-prone JavaScript points are compiled and shared

The above is the detailed content of In-depth understanding of self-executing anonymous functions in Javascript (graphic tutorial). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn