See the following two functions:
Function A () {alert ("")}
VaR B = function () {alert ("B ")}
What are the differences between them? When I open the book, the author will tell us that the former is a function declaration and the latter is a function expression. Function Declaration, as a declaration, will certainly take some action in the pre-compiled class (advance declaration), while function expressions will not. Another difference is that function declarations cannot be executed directly with a pair of parentheses. The third difference is that expressions can be further subdivided. Expressions are composed of constants, variables, operators, and functions. After calculation, a result value is returned, at least one undefined value is returned.
Function A () {alert ("A")} (); // Error
VaR B = function () {alert ("B ")}();
(Function Foo () {}); // expression: note that it is included in the grouping operator var bar = function Foo () {}; // expression, because it is part of the value assignment expression new function bar () {}; // expression, because it is part of the new expression
No error is reported for a pair of parentheses (grouping operators) in the function coat. Parentheses are expressions and expressions have returned values. Therefore, you can add a pair of parentheses to them to execute the statements.
(Function a () {alert ("A")}) (); // No problem
<Br/> (function a () {<br/> alert ("A") <br/>}) (); // No problem <br/>
RunCode
In addition, we know that the function name is used for reference and is useless now. Can we remove it?
(Function () {alert (arguments. callee)}) (); // pop up
<Br/> (function () {<br/> alert (arguments. callee) <br/>}) (); // pop up <br/>
Run code
By knowing this, we can easily recursion ourselves. For example, get a deep copy function.
VaR deepextend = function (destination, source) {for (VAR property in source) {If (source [property] & source [property]. constructor & source [property]. constructor = Object) {destination [property] = destination [property] ||{}; arguments. callee (destination [property], source [property]);} else {destination [property] = source [property] ;}} return destination ;};
In addition, it also explains how to generate various auto-executed functions simultaneously. Because the grouping operator is not necessary to convert a function into an expression, we can also use the void operator ,~ Operator ,! Operator ......
Void function () {alert ("situ zhengmei ");}()
0, function () {alert ("situ zhengmei ");}();
-Function () {alert ("situ zhengmei ");}();
+ Function () {alert ("situ zhengmei ");}();
! Function () {alert ("situ zhengmei ");}();
~ Function () {alert ("situ zhengmei ");}();
Typeof function () {alert ("situ zhengmei ");}();