Immediate execution of javascript function

Have introduced the concept of immediate execution of the function in javascript, then what is immediately execute the function of it? Execute the function immediately is how to write it? Immediately execute the function and normal function What difference does it make?

Let's look at general functions:

function a(){
        var num = 100;
        console.log(num);
    }

These are the most common javascript function defined in the casual, once the function is defined, it will have been waiting to be executed, it will always take up space. Unless wait until executing the javascript function it will be released. Sometimes, however, I do not want so much space is occupied, because waste efficiency. Some functions execute javascript from birth to finish it was executed only once. This results in the function space has been occupied. To avoid this unnecessary space has been occupied, javascript introduced immediate execution functions.

(function test(){
        var a = 12;
        var b = 13;
        console.log(a+b);
    }())

On top of such an approach is to perform the function immediately

 We see that the function can perform normal, but after the execution of this function could not find. So we do not usually perform the functions now take the name to him.

And the same general function as the function to be executed immediately parameters and return values. (Can not have a return value of the function but also it doing ah)

var sum = ( function (x, y, z) {
         return x + y + z; 
    } ( 5,6,7))

 In this case, while executing the function of being destroyed, but the return value is received num. Also solve the problem of memory footprint.

Below are two common function is executed immediately wording :

(Function () {} ()); W3C recommended first
(function (){})();

In the development, execution function is mainly used to solve the problem immediately closures and functions for initialization, etc., used in the initial page does not require immediate execution function returns a value, but at the time of the initial page used immediately execute the function You must have a return value. There are still a function of the immediate implementation of the context of the implementation of the internal implementation of the same precompiled link.

Beyond that the inquiry, 1. Only expression can be performed symbols (parentheses) execution. 2. The expression can be performed symbolic execution of his name will automatically be ignored. What does that mean? See example below

First to explain the top of the first sentence. We know two types of function definition (function declarations and function expressions)

function test(){
     console.log(123);      
}()

Look at the top of the code can execute it?  

Write test represents a function of a single reference to the function you want to perform a direct test () under normal circumstances; parentheses to the function name, the function declaration top of the function reference with the same, that direct plus one pair of parentheses should be able to execute it, the result is not the case:

 In this system it appears to be a very low-level syntax error. Because the front of the parentheses is a function declaration rather than an expression, it can not be executed.

Look at the bottom of this writing:

var a = function (){
     console.log(123);      
}()

This time in front of the parentheses is clearly an expression, so it can be executed.

Second sentence explain upper side.

var a = function (){
     console.log(123);      
}

Now also represents the name of a function it

Implementation of this expression in the following:

var a = function (){
     console.log(123);      
}()

This time is no longer a representation of the function name.

 So: can execute an expression of symbolic execution of his name will automatically be ignored.

 akin:

+function (){
        console.log(123);
    }()
-function (){
        console.log(123);
    }()
!function (){
        console.log(123);
    }()

Several more cases are clearly in front of the expression, so it can be executed. This time it is clear why the overall rate of small brackets can execute it immediately, because it is enclosed in parentheses becomes an expression Well, naturally can be performed.

Guess you like

Origin www.cnblogs.com/daimaxiaocai/p/11990733.html