Immediate execution function expression (self-executing function)

Immediately execute the function expressions

Immediately execute the function expressions, most people also known as self-executing function.

Self-executing functions written

Anonymous function

(function(){
        console.log(2)
    })()

Named function

(function log(){
        console.log(2)
    })()

Since execution of the function parameter passing

(function add(a, b){
        console.log(a + b)
    })(1,2)

return value

let fn =  (function add(a,b){
        return a + b;
    })(2,4);

console.log(fn)

Since execution of the function may be passed as arguments

var a = 2;

(function log(fn){
        fn(window)
    })(function fn(global){
        var a = 3;
        console.log(a); // 3
        console.log(global.a); // 2
    });

Why use immediately execute the function expressions

function add(a, b){
    var c = 2;
    console.log(a + b);
}
add(1,2);

Sometimes we want to achieve needs, in order to achieve these business logic, in order to prevent pollution of the global environment variables defined functions, but these functions, is not waiting for the call, start a program, also followed to perform those functions, but like the above code , although the function of which is not pollute the global variable, but addthe function name variable, but still global pollution, not the United States a bit, so use immediately execute the function expressions can solve this case.

(function add(a, b){
    var c = 2;
    console.log(a + b);
})(1,2)

Precautions

Although the javaScriptsemicolon is optional, but sometimes is necessary.

console.log(5)

(function add(a, b){
    var c = 2;
    console.log(a + b);
})(1,2)

The code above will get an error, because javaScriptthere is indeed a semicolon this rule, it represents the end of a statement.

This time add a semicolon on the line

console.log(5);

(function add(a, b){
    var c = 2;
    console.log(a + b);
})(1,2)

But many times we have become accustomed to not add a semicolon, seemed more trouble, this time we should know under what circumstances the semicolon.

As long as the beginning of the statement is the case of parentheses, square brackets, is the beginning of the slash, plus sign, minus sign in front of the semicolon on the line, of course, you can also add other symbols, such as an exclamation point, but these are generally semicolon

console.log(5)

;(function add(a, b){
    var c = 2;
    console.log(a + b);
})(1,2)

!(function add(a, b){
    console.log(a * b);
})(3,2)

Use self-executing function in the object

var a = 5
var obj = {
    a: 1,
    b: function(){
        console.log(this.a)// 5
    }(),
    c: 4
}
var a = 5
var obj = {
    a: 1,
    b: +function(){
        console.log(this.a)// 5
    }(),
    c: 4
}

Both are above, but note that objects in the immediate execution of the original expressions change thispoint, at this time thisiswindow

var a = 5
var obj = {
    a: 1,
    b: function(){
        console.log(this.a)// 1
    },
    c: 4
}
obj.b()

This time, thispointingobj

Guess you like

Origin www.cnblogs.com/tourey-fatty/p/12114649.html