Function declaration and function expressions in Javascript)

Source: Internet
Author: User
Tags variable scope

For example:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

If you try this code, it means to declare a function and execute it immediately. Because the variable scope in Javascript is based on the function, this can avoid variable pollution, but here the bitwise operator "~" At first glance, people cannot understand it. If you remove it and run it again, an error will be reported: SyntaxError.

Before explaining why, let's clarify two concepts in Javascript: function declaration and function expression:

Let's take a look at the function declaration:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Let's take a look at what kind of function expressions are:
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Now let's look back at the question at the beginning of the article. Why do we remove the bitwise operator "~"? An error is reported during subsequent running. This is because Javascript cannot directly use parentheses after function declaration from the perspective of Syntax Parsing, but function expressions do not have this restriction, add "~" before the function declaration Operator, you can let the syntax parser regard it as a function expression. Similarly, add "!, +,-"And other operators are also feasible.

So why don't we use the following function expression?


[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

Although there is no problem from the perspective of Syntax Parsing, the above Code has drawbacks. It introduces a variable, which may pollute the existing runtime environment and bring potential problems.

Use the bitwise operator "~" In fact, the function declaration is more readable with parentheses:


[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]

After understanding the principle, no matter what writing method you encounter, you will not be confused by the second monk.

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.