Javascript anonymous function and its code pattern principle _javascript Skill

Source: Internet
Author: User
Tags anonymous
about what is anonymous function, and its advantages in this article is not delve into, first throw a common anonymous function:
(function () {alert (' Yo ')}) ()
Many students know how to use this anonymous function, but may not understand why it is written so that anonymous functions can be invoked. Perhaps knowing that the following parentheses are performing the preceding function is not clear what the preceding parentheses means! This article will show you how the code pattern of anonymous functions works.
OK, let's take a look at more anonymous function invocation patterns:
Copy Code code as follows:

(function () {alert (1);} ())
(function () {alert (2);}) ()
void function () {alert (3);} ()

All 3 of these are correct and functionally equivalent.
Then look at the wrong way of writing:
4.
function () {alert (' Yo ')} ()
The above code throws a syntax error. With this question, let's take a look at the answers given by Qin Ge:
1. Function literal: Declare a Function object first, and then execute it.
2. Precedence expression: Because the JavaScript execution expression is from inside the parentheses to the outside, the declared function can be enforced with parentheses.
3.void operator: A void operator is used to perform a single operand that is not surrounded by parentheses.
Well, let's put this aside, regardless of whether the answer is correct, and then look at the syntax of the function declaration and the syntax description of the function expression:
First, function declaration:
Copy Code code as follows:

function name ([param[, param[, ... param]]]) {
Statements
}

The name of the function here is not to be omitted. If the function name is omitted, an error is made.
It also makes sense to explain why you write directly
function () {alert (' Yo ')}
Going to go wrong? Because the compiler is a function declaration and the Code does not appear in the function name, the result is conceivable.
Second, the function of expression:
Copy Code code as follows:

function [name] ([param] [, param] [..., param]) {
Statements
}

The name of the function can be omitted, and omitting name is called an anonymous function. Note: If you need to create an anonymous function, you must give an explicit declaration of a function expression rather than a function
Now we can make a more accurate explanation:
The 1\2\3 anonymous function code pattern is simply explained by parentheses or void telling the compiler to use function () {} As a functional expression. There is no such complex precedence and void operator principle. It's just a simple syntax conversion.
It can be imagined that, as long as the syntax of a function expression is met, we can create an n-anonymous function code pattern, such as
Copy Code code as follows:

!! function () {
Alert (' Yo ');
}()
+function () {
Alert (' Yo ');
}()

Wait a minute ...
There is no deeper mystery, just the difference between a function declaration and an expression, and if you don't understand it, maybe you're thinking more complicated.
We now learn JavaScript has no special professional training arrangements and courses, the improvement of coding ability is to rely on their own peacetime practice and accumulation. However, the more this is easier to ignore the most basic knowledge, and finally complicate the simple problem, not desirable. Some students are holding the ability to use the attitude of learning JavaScript, and do not go into the deep, know it but do not know why, in fact, to the final ability and not much improvement ... Not to be taken!
Extended 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.