Home > Web Front-end > JS Tutorial > body text

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

亚连
Release: 2018-05-21 11:04:22
Original
1429 people have browsed it

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(){
//代码
})();
Copy after login

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。
Copy after login

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

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

This code is equivalent to

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

a(jQuery);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template