Parse JavaScript midpoint number "." The Polysemy _javascript skills

Source: Internet
Author: User

Point number "." There are two kinds of semantics in JavaScript

Semantics 1, representing the decimal point in arithmetic (floating-point number), such as 2.5

Semantic 2, Object properties, methods, such as [].push (2)

There is hardly any difficulty in understanding this, but the following question is interesting.

Copy Code code as follows:

How will this line of code execute
1.toString ();

Firebug in the following



The point number here is expressed in the above semantics 1, so the dot must be followed by a number, here with ToString, the report syntax is wrong.

The workaround is simple, such as adding parentheses

Copy Code code as follows:

(1). ToString ();

You can write that, but it's hard to understand.
Copy Code code as follows:

1..toString ();

The reason why the browser can run, because the browser JS engine to "1..toString ()" understood as "1.0.toString ()". The first point here is semantic 1, and the second point is the semantics 2.

And even more bizarre, and there's no error.

Copy Code code as follows:

1. toString (); Note that there is a space in front of the dot number

Obviously, the point number here is Semantic 2, that is, the JS engine ignores the spaces in front of the dot operator, regardless of whether the space before or after is ignored. As follows
Copy Code code as follows:

1. ToString (); There is a space before and after the dot number
1. ToString (); There are two spaces before and after the dot number
1. toString (); There is a tab before the dot number
1. ToString (); There is a tab before and after the dot number

JS engine not only ignores spaces, but also ignores tab.

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.