调用数字文字的成员函数 [英] Calling member function of number literal

查看:106
本文介绍了调用数字文字的成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试调用文字函数,但我得到了奇怪的行为。

I'm trying to call literal functions, but I get weird behavior.

考虑这段代码返回 true

   23 === (23)

当我写下时尝试以下内容。

When I write try the following.

(23).toFixed(2)

我得到了预期的结果 _23.00 _ 但是当我尝试 23.toFixed(2)时出现此错误。

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


SyntaxError:意外的标记ILLEGAL

SyntaxError: Unexpected token ILLEGAL

JavaScript如何评估无法理解的表达式以及为什么会出现此错误?

How does JavaScript evaluate expressions that cannot understand this and why do I get this error?

推荐答案

Greg Hewgill icktoofay 在所有方面都是正确的,但是,我想要抽象一点,抽象明智:让我们看看究竟发生了什么根据javascript规范。

The answers by Greg Hewgill and icktoofay are correct in all ways, however, I'd like to get down a bit, abstraction-wise: Let's see what's really happening according to the javascript specification.

第7.8.3节规范定义了数字文字。我们可以看到以下内容:

Section 7.8.3 of the spec 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 ,一个数字,是一串十进制数字,可能后跟一个点,其后可能跟着其他数字(例如,所有这些数字后跟一个指数, e12 )。换句话说, 42。是合法的,等于 42 3e2 等于 300

A DecimalLiteral, a number, is a bunch of decimal digits, possibly followed by a dot, which is possibly followed by other digits (all of which can be followed by an exponent, e12 for instance). In other words, 42. is legal and equal to 42 and 3e2 is equal to 300.

注意如果我们有一个点,我们要么预计它是接着是更多的数字/指数,或者后面没有任何数字。但是,这是重要的部分,点是数字的一部分。请记住这一点,我们将看看如何处理点运算符 obj.prop

Note how if we have a dot, we either expect it to be followed by more digits/exponent, or be followed by nothing. However, and this is the important part, the dot is part of the number. Remember this as we move to look how the dot operator, obj.prop, is dealt with.

第11.2.1节属性访问者描述了成员访问的点和括号表示法:

Section 11.2.1, Property Accessors describes the dot and bracket notation for member access:

MemberExpression . IdentifierName

CallExpression 用于函数调用,我们不在乎。注意我们如何期待 MemberExpression (可以是 DecimalLiteral - 但是不要相信我的话,看看我是否对。)

CallExpression is for function calls, which we don't care about. Notice how we're expecting a MemberExpression (which can be a DecimalLiteral - but don't take my word for it, look and see whether I'm right).

看到那个小点?向前跳并说好吧,这里的方案中有一个点......在 4.foo 中有一个点......所以为什么会有错误?唉,我用这些句子的假想朋友,你忘记了 DecimalLiteral 的样子!让我们看看两个例子,看看会发生什么。

See that little dot? It's logical to jump forward and say "well, there's a dot in the scheme here...and there's a dot in 4.foo...so why is there an error?" Alas my hypothetical friend whom I use for these sentences, you forgot how the DecimalLiteral looks like! Let's go over two examples and see what happens.

42.foo
^

插入符代表我们所处的角色。到目前为止,我们在 DecimalLiteral / DecimalIntegerLiteral / NonZeroDigit 里面(这是非常令人满意的)。让我们转到下一个字符:

The caret represents the character we're on. So far, we're inside DecimalLiteral / DecimalIntegerLiteral / NonZeroDigit (that's quite a mouthful). Let's move to the next character:

42.foo
 ^

仍然是数字的一部分,一个完全有效的 DecimalDigit

Still part of the number, a perfectly valid DecimalDigit.

42.foo
  ^

好的,所以我们不在 DecimalIntegerLiteral 部分。以下是该方案的相同图表:

ok, so we're out of the DecimalIntegerLiteral part. Here's the same diagram on the scheme:

DecimalIntegerLiteral . DecimalDigits(opt) ExponentPart(opt)
                      ^

所以我们在一个点上,这是一个完全有效的部分。现在我们消费它,作为数字的一部分,继续前进:

So we're on a dot, which is a perfectly valid part of a number. Now we consume it, as part of the number, and move on:

42.foo
   ^

f 既不是 DecimalDigits 也不是 ExponentPart ,我们现在已超出数字。所以现在怎么办?什么是 f ?这不是任何事情的一部分。也许它是一个属性访问者?我们来看看这个方案:

f is neither part of DecimalDigits nor of ExponentPart, we're out of the number now. So...what now? What's that f? It's not part of anything. Maybe it's a property accessor? Let's take a look at the scheme:

MemberExpression . IdentifierName
      ^

我们肯定在 MemberExpression ,但我们后面没有一个点 - 该点已经是数字的一部分。我们遇到了一个语法错误:我们停止执行并抛出它。希望你不住在玻璃房子里。

We're definitely on MemberExpression, but we don't have a dot which follows it - that dot is already part of the number. We've reached a syntactical error: we stop execution and throw it. Hopefully you don't live in a glass house.

希望现在你明白为什么 42..foo 有效。一旦我们离开 MemberExpression ,我们将面临另一个点:

Hopefully now you understand why 42..foo works. Once we're out of the MemberExpression, we face another dot:

              42..foo
                 ^
MemberExpression . IdentifierName
                 ^

后面跟一个完全合法的 IdentifierName

Followed by a perfectly legal IdentifierName.

当然,还有其他几种方法可以将点与数字分开。正如您所示,一种方法是在括号中包围文字:(42).foo 。当我们到达括号结束时,我们已经超出 MemberExpression ,并且在点上。另一种方法是插入一个空格: 42 .foo ,因为空格不能是数字的一部分,而且它对于解析器来说是中性的,所以它不会引发错误。

Of course, there're several other ways to separate the dot from the number. One way, as you showed, is to surround the literal in parentheses: (42).foo. When we've reached the parentheses end, we're out of the MemberExpression, and on the dot. Another way is to insert a space: 42 .foo, since a space can't be part of the number, and it's neutral for the parser, so it won't raise an error.

这篇关于调用数字文字的成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆