为什么-1 ** 2 javaScript中的语法错误?

发布于 2025-01-29 02:12:50 字数 325 浏览 3 评论 0 原文

在浏览器控制台中执行它,它说 SyntaxError:意外的令牌** 。 在节点中尝试它:

> -1**2
...
...
...
...^C

我认为这是算术表达式,其中 ** 是电源运算符。其他运营商没有这样的问题。

奇怪的是,在第二行上键入*/触发执行:

> -1**2
... */
-1**2
  ^^
SyntaxError: Unexpected token **

这里发生了什么?

Executing it in the browser console it says SyntaxError: Unexpected token **.
Trying it in node:

> -1**2
...
...
...
...^C

I thought this is an arithmetic expression where ** is the power operator. There is no such issue with other operators.

Strangely, typing */ on the second line triggers the execution:

> -1**2
... */
-1**2
  ^^
SyntaxError: Unexpected token **

What is happening here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遮了一弯 2025-02-05 02:12:50

在浏览器控制台中执行它是SyntaxError:意外的令牌**。

因为那是 spec 。设计这种方式是为了避免混淆它是一个(即(-1)** 2 )的否定的平方,还是一个正方形的否定(IE - (1) ** 2))。该设计是广泛讨论对操作员优先级的广泛讨论,并检查如何处理此操作的方法。在其他语言中,最后决定通过将其作为语法错误来避免出乎意料的行为。

Executing it in the browser console says SyntaxError: Unexpected token **.

Because that's the spec. Designed that way to avoid confusion about whether it's the square of the negation of one (i.e. (-1) ** 2), or the negation of the square of one (i.e. -(1 ** 2)). This design was the result of extensive discussion of operator precedence, and examination of how this is handled in other languages, and finally the decision was made to avoid unexpected behavior by making this a syntax error.

眼眸 2025-02-05 02:12:50

来自::

在JavaScript中,不可能编写模棱两可的指示表达式,即您不能放置一个单一操作员(+// - // // delete / void / typeof )在基本号码之前。

原因也在同一文本中解释:

在大多数语言中,例如PHP和Python等具有指示操作员的其他语言(通常^ ** ),指定的型运算符被定义为比Unary +和Unary - 之类的Unary操作员,但是有一些例外。例如,在bash中, ** 操作员的定义为比单位运算符的优先级低。

因此,为避免混乱,决定代码必须删除歧义并明确放置括号:

(-1)**2

或:

-(1**2) 

作为旁注, binary - 未对此进行处理 - - 具有较低的优先级 - 因此,最后一个表达式具有与此有效表达式相同的结果:

0-1**2

其他编程语言中的指示优先级

,如上所述,大多数具有Infix Exponention Operator的编程语言,对该操作员给予更高的优先级而不是一般的负。

以下是一些编程语言的其他示例,这些

From the documentation on MDN:

In JavaScript, it is impossible to write an ambiguous exponentiation expression, i.e. you cannot put a unary operator (+/-/~/!/delete/void/typeof) immediately before the base number.

The reason is also explained in that same text:

In most languages like PHP and Python and others that have an exponentiation operator (typically ^ or **), the exponentiation operator is defined to have a higher precedence than unary operators such as unary + and unary -, but there are a few exceptions. For example, in Bash the ** operator is defined to have a lower precedence than unary operators.

So to avoid confusion it was decided that the code must remove the ambiguity and explicitly put the parentheses:

(-1)**2

or:

-(1**2) 

As a side note, the binary - is not treated that way -- having lower precedence -- and so the last expression has the same result as this valid expression:

0-1**2

Exponentiation Precedence in Other Programming Languages

As already affirmed in above quote, most programming languages that have an infix exponentiation operator, give a higher precedence to that operator than to the unary minus.

Here are some other examples of programming languages that give a higher precedence to the unary minus operator:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文