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:
You can write that, but it's hard to understand.
Copy Code code as follows:
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.