Calling member function of a numeric literal

I try to call literal functions, but I get weird behavior.

Consider this code that returns true .

  23 === (23) 

When I write, try the following.

 (23).toFixed(2) 

I get the expected result _23.00_ , but when I try 23.toFixed(2) , I get this error.

SyntaxError: Unexpected ILLEGAL Token

How does JavaScript evaluate expressions that cannot understand this and why am I getting this error?

+47
javascript
Sep 01 '13 at 6:27
source share
3 answers

The answers of Greg Hewgill and icktoofay are true in every way, however I would like to go down a bit, abstract myself: let's see what actually happens according to the javascript specification.

Section 7.8.3 of the specification defines numeric literals. We can see the following:

 DecimalLiteral :: DecimalIntegerLiteral . DecimalDigits(opt) ExponentPart(opt) . DecimalDigits ExponentPart(opt) DecimalIntegerLiteral ExponentPart(opt) DecimalIntegerLiteral :: 0 NonZeroDigit DecimalDigits(opt) 

A DecimalLiteral , a number is a group of decimal digits, possibly followed by a period, possibly followed by other digits (which can be followed, for example, by the exponent e12 ). In other words, 42. is legal and is 42 , and 3e2 is 300 .

Please note that if we have a dot, we either expect that it will be followed by a larger number of digits / exhibitors, or nothing will follow. However, and this is an important part, the dot is part of the number. Keep this in mind when we move on to examining how the point operator, obj.prop , is obj.prop .

Section 11.2.1, Property Accessors, describes the notation of dots and brackets for member access:

 MemberExpression . IdentifierName 

CallExpression designed to call functions that we do not need. Please note that we are expecting MemberExpression (which may be DecimalLiteral - but don't take my word for it, look and make sure I'm right).

See this little dot? It is logical to jump forward and say "well, there is a point in the circuit here ... and there is a point in 4.foo ... so why is there an error?" Alas, my hypothetical friend whom I use for these offers, you forgot what DecimalLiteral looks DecimalLiteral ! Let's move on to two examples and see what happens.

 42.foo ^ 

The carriage represents the symbol in which we are. So far, we are inside DecimalLiteral / DecimalIntegerLiteral / NonZeroDigit (that's quite a lot). Go to the following symbol:

 42.foo ^ 

Still part of the number, perfectly correct DecimalDigit .

 42.foo ^ 

ok, so we left the DecimalIntegerLiteral part. Here is the same diagram in the diagram:

 DecimalIntegerLiteral . DecimalDigits(opt) ExponentPart(opt) ^ 

So, we are at the point, which is a very real part of the number. Now we consume it, as part of the number, and move on:

 42.foo ^ 

f is neither part of DecimalDigits nor ExponentPart , now we are behind the number. And now what? What is this f ? This is not part of anything. Maybe this is an access property? Let's look at the diagram:

 MemberExpression . IdentifierName ^ 

We are definitely on MemberExpression , but we don’t have a dot that follows it β€” that dot is already part of the number. We have reached a syntax error: we stop execution and throw it. I hope you do not live in a glass house.

Hopefully now you will understand why 42..foo works. When we MemberExpression , we will MemberExpression another point:

  42..foo ^ MemberExpression . IdentifierName ^ 

What follows is a perfectly legitimate IdentifierName .

Of course, there are several other ways to separate a point from a number. One way, as you have shown, is to surround literal parentheses: (42).foo . When we reached the end of the parentheses, we left MemberExpression and point. Another way is to insert a space: 42 .foo , since space cannot be part of a number, and it is neutral for the parser, so it does not cause an error.

+57
Sep 01 '13 at 8:13
source share

Unlike Ruby (for example), the Javascript parser considers . The following numbers, which should be part of the number. Therefore, the parser sees markers:

23. toFixed ( 2 )

which is a syntax error, because the word toFixed , immediately following the floating point number, does not make sense. A language such as Ruby that accepts this syntax will see the following tokens:

23 . toFixed ( 2 )

+35
Sep 01 '13 at 6:29
source share

Consider:

 5. 

Is this a floating point literal 5. or an integer 5 followed by a period? You do not know; this is ambiguous. JavaScript takes the first view. In the JavaScript view, you have a floating point literal followed by an identifier (then followed by a left bracket, a number, and a right bracket).

Some people work around this using two points:

 23..toFixed(2) 

Since a floating point literal can have only one decimal point, the other point is a dot symbol.

+24
01 Sep '13 at 6:30
source share



All Articles