JS (function () {...}) () immediately execute function comprehension
JavaScript is more casual than other programming languages, so the JavaScript code is filled with a variety of exotic writing, sometimes mirrors, and of course, the ability to understand the various types of writing is a further understanding of the JavaScript language features.
(function () {...}) () and (function () {...} ()) is a common way of writing two JavaScript functions immediately, initially I thought it was a parenthesis wrapping an anonymous function, followed by parentheses to call a function, and finally to the function definition immediately after the purpose of execution, and later found that the reason for parentheses is not so. To understand the immediate execution of a function, you need to first understand some basic concepts of functions.
function declarations, function expressions, anonymous functions
function declaration: Functions fnname () {...}; Declare a function using the function keyword, and then specify a name for the functions, called the function declaration.
function expression var fnname = function () {...}; Declaring a function with the function keyword, but not naming it, and finally assigning an anonymous function to a variable called a function expression, is the most common form of function expression syntax.
Anonymous functions: Function () {}; Declaring a function using the functions keyword, but not naming the function, so called anonymous functions, anonymous functions are function expressions , anonymous functions have many functions, assigning a variable to create a function, assigning an event to an event handler or creating a closure, and so on.
The difference between a function declaration and a function expression is that the JavaScript engine will ' function declaration elevation ' (function declaration hoisting) Functions declaration on the current execution environment (scope) when parsing JavaScript code. The function expression must wait until the JAVASCIRTP engine executes to its row to parse the function expression from the top and the next line, and the function expression can be called immediately after it is appended with parentheses, and the function declaration cannot be called only in the form of FnName (). Here are two examples of the difference between the two.
Help
1234567891011 |
fnName(); function fnName(){ ... } //正常,因为‘提升’了函数声明,函数调用可在函数声明之前 fnName(); var fnName= function (){ ... } //报错,变量fnName还未保存对函数的引用,函数调用必须在函数表达式之后 |
Help
12345678910111213 |
var fnName=
function
(){
alert(
‘Hello World‘
);
}();
//函数表达式后面加括号,当javascript引擎解析到此处时能立即调用函数
function fnName(){
alert(
‘Hello World‘
);
}();
//不会报错,但是javascript引擎只解析函数声明,忽略后面的括号,函数声明不会被调用
function
(){
console.log(
‘Hello World‘
);
}();
//语法错误,虽然匿名函数属于函数表达式,但是未进行赋值操作,
//所以javascript引擎将开头的function关键字当做函数声明,报错:要求需要一个函数名
|
After understanding some basic concepts of function, look back (function () {...}) () and (function () {...} ()) These two functions are immediately executed, initially I thought it was a parenthesis wrapped anonymous function, and the following parentheses immediately call the function, then do not know why the parentheses, and then understand that the function body after the parentheses can be called immediately, the function must be a function expression, not a function declaration .
Help
1234567891011121314151617181920212223 |
(
function
(a){
console.log(a);
//firebug输出123,使用()运算符
})(123);
(
function
(a){
console.log(a);
//firebug输出1234,使用()运算符
}(1234));
!
function
(a){
console.log(a);
//firebug输出12345,使用!运算符
}(12345);
+
function
(a){
console.log(a);
//firebug输出123456,使用+运算符
}(123456);
-
function
(a){
console.log(a);
//firebug输出1234567,使用-运算符
}(1234567);
var fn=
function
(a){
console.log(a);
//firebug输出12345678,使用=运算符
}(12345678)
|
You can see the output and add it in front of the function! , +,-even a comma wait for the effect to be executed immediately after the function definition, and (),! , + 、-、 =, and so on, all function declarations are converted into function expressions , eliminating the ambiguity of the JavaScript engine recognition function expressions and function declarations, telling the JavaScript engine that this is a function expression, not a function declaration, you can add parentheses later, And immediately executes the code for the function.
Parentheses are the safest thing to do, because! , +,-et operators also operate on the return values of functions, sometimes causing unnecessary hassles.
But what is the use of such a notation?
The concept of a private scope in JavaScript, if you declare some variables in a global or local scope on a multi-person project, may be overridden by someone else accidentally with a variable of the same name, depending on the nature of the scope chain of the JavaScript function, You can use this technique to mimic a private scope, use an anonymous function as a "container," inside the container to access external variables, and the external environment to access variables inside the container, so (function () {...}) () internally defined variables do not collide with external variables, commonly known as "anonymous wrapper" or "namespace."
jquery uses this method to wrap the jquery code in the (function (window,undefined) {...} jquery code ...} (window), when you invoke jquery code in the global scope, you can achieve the protection of jquery internal variables.
This article is a personal understanding of the collation, if there are errors in the welcome point of view, the text of the views from:
The JavaScript authority Guide, JavaScript advanced programming
JavaScript executes functions immediately