Javascript anonymous functions and code pattern principles

Source: Internet
Author: User

This article will not go into the details about what an anonymous function is and its advantages bring about. First, we will throw a common anonymous function:
(Function () {alert ('yo ')})()
Many people know how to use this anonymous function, but they may not understand why this write can call the anonymous function. You may know that the parentheses behind them are the execution of the previous function, but you may not know the meaning of the parentheses! This article will show you how anonymous functions work in code mode.
OK. Let's take a look at more anonymous function call modes:
Copy codeThe Code is as follows:
(Function () {alert (1 );}())
(Function () {alert (2 );})()
Void function () {alert (3 );}()

All three are correct and functionally equivalent.
Let's take a look at the wrong writing method:
4.
Function () {alert ('yo ')}()
The above code throws a syntax error. Why? Let's take a look at the answers provided by Qin Ge with this question:
1. Function literal: first declare a function object and then execute it.
2. Priority expression: Since the Javascript Execution expression is from the inside of the parentheses to the outside, you can use parentheses to forcibly execute the declared function.
3. void Operator: Use the void operator to execute a separate operand without parentheses.
Well, whether the answer is correct or not, let's put these aside and look at the syntax of the function declaration and the syntax description of the function expression:
I. function declaration:
Copy codeThe Code is as follows:
Function name ([param [, param [,... param]) {
Statements
}

The function name cannot be omitted here. If the function name is omitted, an error is returned.
This also explains why to write directly
Function () {alert ('yo ')}
Error? This is because when the compiler declares a function and no function name appears in the Code, the result can be imagined.
Ii. function expressions:
Copy codeThe Code is as follows:
Function [name] ([param] [, param] [..., param]) {
Statements
}

The function name can be omitted. The omitted name is called an anonymous function. Note: To create an anonymous function, you must provide a function expression instead of an explicit declaration of the function.
Now we can make a more accurate explanation:
1 \ 2 \ 3 the anonymous function code mode only tells the compiler via parentheses or void, and treats function () {} as a function expression. In this case, there is no complicated principle of priority and void operator. This is just a simple syntax conversion.
As you can imagine, as long as it complies with the syntax of the function expression, we can create N anonymous Function Code modes, such
Copy codeThe Code is as follows:
!! Function (){
Alert ('yo ');
}()
+ Function (){
Alert ('yo ');
}()

Wait ......
There is no deeper mystery, but the difference between a function declaration and an expression. If you do not understand it, it may be that you think more about it.
We do not have any special training arrangements or courses to study Javascript. The improvement of coding capability depends on our own practice and accumulation. However, the more you do, the more likely you will be to ignore the most basic knowledge. In the end, you will complicate the simple problem and make it unavailable. Even more, some students are able to learn Javascript with an attitude that they can use, instead of going deep into it. However, they do not know why. In fact, they have not improved much in the end ...... Not desirable!
Additional reading:
What do parentheses surrounding a JavaScript function declaration mean?

Related Article

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.