Summary of Javascirpt Anonymous self-execution function

Source: Internet
Author: User
Tags anonymous

See a lot of the site's JS file, the first function to face is usually an anonymous self-execution function, such as (function () {}), its role is two, one is anonymous, in Java, there are anonymous internal classes, the role is similar. Anonymous means that there is no name for the function, which makes it impossible to access the inside of the anonymous self-execution function outside of the function. To prevent the interaction between the names of variables, such as two people wrote two JS, placed in the same Web page, if they do not use anonymous functions wrapped, the two JS files between the function will call each other, Lead to the creation of errors.

So how do you access the functions inside the anonymous function? Usually in the inside of the function or variable before the window, so that the function or variable becomes global, on the same page, even if it is different from my JS file, you can access, so as long as a page to add jquery.js, then other JS files can be used jquery (*) To access the functions in Jquery.js.

Another function of the anonymous self execution function is self execution, in fact, as long as in the JS file in any code, as long as does not include the function type, it will perform, it is a bit like the onload, but the onload is in the page loaded after the implementation, it does not, so if it wants to call a page element , be sure to place this page element before the anonymous self execution function, or you will generate an error.

1. What is a self executing anonymous function?

It refers to a function such as this: (function {//code});


2. Questions

Why (function {//code}); can be executed, and function {//code} ();


3. Analysis

(1). First, be clear about the difference between the two:
(function {//code}) is an expression, function {//code} is a declaration of functions.
(2). Second, JS "precompiled" features:
JS in the "precompiled" phase, will interpret the function declaration, but will be a slight.
(3). When JS executes to function () {//code} (), because function () {//code} has been interpreted in the precompiled phase, JS skips function () {//code} to attempt to execute ();
When JS executes to (function {//code}), because (function {//code}) is an expression, JS will be able to solve it to get the return value, because the return value is a function, and therefore encountered (), it will be executed.


In jquery, anonymous self-execution functions are nested in the anonymous self-execution function, in the form

The code is as follows Copy Code

(function () {

var a=function () {};

(function () {

A ()

})();

})();

This is the current implementation of the self invocation, anonymous self execution function in the anonymous function can call the superior function. This is also a bit like Java, where variables and methods within a class are transparent to the inner class. So we don't have to add windows before a=function A, so we look down.


One way to declare a function:

The code is as follows Copy Code

var afun = function () {

Alert (1);

};

Afun is a function that is accurate to a variable or pointer to a function, and if it is not written afun, then:

The code is as follows Copy Code

function () {

Alert (1);

};

is an anonymous function, because there is no name, but it does exist in memory. A function without a name we have no way to call it directly, calling a function requires a function name plus a pair of parentheses, such as Afun (). What can the anonymous function do? Although there is no name, it can pass itself as a variable to other functions, such as:

The code is as follows Copy Code

var fun = function (afunction) {

if (typeof afunction = = "function") {

Afunction ();

}

}

Fun (function () {

Alert (1);

});

Although there is no name in the definition of this function, it is passed to fun and, in the fun function, it has a name, and the name is afunction. This usage is actually very common, and then for example:

The code is as follows Copy Code

Dojo.connect ("Adiv", "onclick", function () {

Alert ("onclick");

});

Another use of anonymous functions is self execution. After the function name is followed by a pair () is to execute the function, the following bracket expression is defined as a function, and the return value of the bracket expression is this function:

The code is as follows Copy Code

(function () {

Alert (1);

});

Outside this pair of parentheses followed by () is the function that performs the parentheses expression return:

The code is as follows Copy Code

(function () {

Alert (1);

})();

This is the anonymous self execution function, first it has no name, secondly, it is executed directly after the definition, and it cannot be executed again because it has no name. Anonymous self-execution functions can be used to pass parameters:

The code is as follows Copy Code

(function (i) {

alert (i);

}) (1);

Anonymous self-execution functions have many uses, mainly for encapsulation, such as looking at jquery or Dojo's source code found that they are encapsulated in the anonymous self execution function, so that the page can be automatically initialized when loading, and exposing the properties and methods it wants to expose, The temporary variables used can be safely closed within the anonymous function.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.