Detailed JavaScript syntax for {} Treatment of the pit Dad _ basics

Source: Internet
Author: User
Tags object object

JavaScript has a lot of holes in its grammar, which is known to all.

Let's take a picture first.

The code is as follows:

Copy Code code as follows:

{} + []; 0
[] + {}; "[Object Object]"
{} + [] == [] + {}; False
({} + [] == [] + {}); True

The grammatical pit of such an egg ache is estimated to be only a wonderful thing like JavaScript.

I believe that most of the children do not study the JavaScript compiler of the child's shoes, simply can not understand. (At least I think it's incredible)

Later a special go to the Niang a bit, just a little enlightened!

Now let's look at this code:

Copy Code code as follows:

{
A:1
}

Believe that most children's shoes, the first eye will think that this is a direct amount of objects.

What about this code?

Copy Code code as follows:

{
var a = 1;
}

Does the browser prompt for syntax errors?

Obviously not! Think it over, and we'll see that this is a statement block.

Copy Code code as follows:

if (isnumber) {
var a = 1;
}


Speaking of which, the keen you may have found: JavaScript in the beginning of {, there will be ambiguity.

What does the JavaScript compiler do with this ambiguity?

To solve this problem, the ECMA method is very simple and rough: when parsing, if a statement begins with a "{", it is interpreted as a block of statements.

This is really a pit dad's way of handling!

Since it is a statement block, then why {a:1} but no grammatical errors?

In fact, here, a is understood by the parser as a label. tags are used to match break and continue statements for directional jumps.

As a result, such a notation throws an exception:

Copy Code code as follows:

{
A:function () {}
}

The function () {} is not a function declaration, nor is it an expression.

Here, we should have a basic concept of the wonderful processing of {}. Let's look at some of the statements that were mentioned at the beginning of the article:

Copy Code code as follows:

{} + []; 0
[] + {}; "[Object Object]"
{} + [] == [] + {}; False
({} + [] == [] + {}); True

First, because {} is a statement block, the code can be understood to be:

Copy Code code as follows:

if (1) {}
+[]

So the return value is 0.

The second, because {} is not at the beginning of the statement, is a normal object direct quantity, empty array and empty object add directly, return "[Object]".

To understand the first and second article, the third article has no need to explain.

Fourth, since it is () started, the first {} is resolved to the object's direct amount, so that the two formulas are equal, and return true.

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.