"You do not know JavaScript (on)," notes - block scope and function scope

About function declaration: If the function is declared in the first word, then that is a function declaration, otherwise, it is a function expression. For example, an anonymous function in this form, the function will be treated as a function of expression rather than a standard function declaration to deal with.

(function(){
    // 立即执行函数表达式
})()

Another use of this expression has an inverted code sequence run, the function needs to run the second place, after performing IIFE passed as parameters into account.

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

Guess you like

Origin www.cnblogs.com/simpul/p/11027200.html