Automatic function execution

Source: Internet
Author: User

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 ");}();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.