JavaScript幂运算一元运算符的设计决策 [英] JavaScript exponentiation unary operator design decision

查看:92
本文介绍了JavaScript幂运算一元运算符的设计决策的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正与新的幂运算符鬼混,发现不能在基数前紧跟一元运算符.

So I was fooling around with the new exponentiation operator and I discovered you cannot put a unary operator immediately before the base number.

let result = -2 ** 2; // syntax error
let result = -(2 ** 2); // -4
let x = 3;
let result = --x ** 2; // 4

摘自MDN上的文档:

在JavaScript中,不可能编写模棱两可的幂表达式,即不能放置一元运算符(+/-/~/!/delete/void/)紧接在基数之前.

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.

在大多数语言中,例如PHP和Python以及其他具有指数运算符的语言(通常为^**),指数运算符的定义优先级高于一元运算符(例如一元+和一元-,但也有一些例外.例如,在Bash中,**运算符被定义为具有比一元运算符更低的优先级.

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.

我知道这是设计使错误.我不明白这个设计决定.谁真的会对-x ** 2为负数感到惊讶?这不仅遵循了其他主流编程语言,而且遵循了数百年来普遍使用的数学符号,并且这种数学符号已向每位高中代数学生教授.

I understand this was made an error by design. I don't understand this design decision. Who's really going to be surprised that -x ** 2 is negative? This follows not only other mainstream programming languages but a mathematical notation that has been in common use for hundreds of years and is taught to every high school algebra student.

在Javascript中,'1'+ 2'12',而'1'-2-1,但是-1**2会引发错误,因为它可能是模棱两可的?帮助我了解这个设计决定.

In Javascript '1'+ 2 is '12' and '1'-2 is -1 but -1**2 raises an error because it could be ambiguous? Help me understand this design decision.

推荐答案

我不明白这个设计决定.

I don't understand this design decision.

有关详细信息,请访问 https://esdiscuss.org/topic/exponentiation-operator-precedence https://esdiscuss.org/topic/power-operator-why-does-2-3-throws https://github.com/rwaldron/tc39-notes/blob/master/es7/2015-09/sept-24.md#exponentiation-operator .

Read more about it at https://esdiscuss.org/topic/exponentiation-operator-precedence, https://esdiscuss.org/topic/power-operator-why-does-2-3-throws, https://github.com/rwaldron/tc39-notes/blob/master/es7/2015-09/sept-23.md#exponentiation-operator and https://github.com/rwaldron/tc39-notes/blob/master/es7/2015-09/sept-24.md#exponentiation-operator.

-x ** 2是否为负真的会让谁感到惊讶?

Who's really going to be surprised that -x ** 2 is negative?

足够重要的人.以上资源中的一些相关引用:

Enough people to matter. Some relevant quotes from the above resources:

  • "making ** bind tighter than unary operators would break x**-2. And making it sometimes tighter and sometimes looser would be too confusing and lead to other opportunities for precedence inversion." - Waldemar Horwat
  • "Given the conflict between the history of ** in other languages, [and] the general pattern that unary binds tighter than binary, any solution at this point will confuse many people." - Mark S. Miller
  • "acknowledge the prospect of significant whitespace: -x**2 === -(x ** 2) and -x ** 2 === (-x) ** 2" - Alexander Jones
  • "The problem is, however rare unary minus before an exponentiation expression may be, the lack of superscript-with-smaller-font sugests that - binds tighter than **. And indeed apart from dot (a special form whose right operand must be a lexical identifier-name) and square brackets (which isn't an infix operator per se), unary operators bind tighter than binary in JS as in C and other C-derived languages." - Brendan Eich
  • "For math it seems obvious that -52. But for -5 ** 2, because of the whitespace around the infix operator. Even without space, - seems to be part of the literal." - Dave Herman
  • [Regarding programming language precedence], "effectively zero people have an intutition about this from other languages. Agree people have an itutition that ** is the exponentiation operator. But people usually try to avoid dark corners so they never develop an intuition for negative bases." - Dave Herman

在Javascript中,'1'+ 2'12',而'1'-2-1,但是-1**2会引发错误,因为它可能是模棱两可的?

In Javascript '1'+ 2 is '12' and '1'-2 is -1 but -1**2 raises an error because it could be ambiguous?

他们今天在语言扩展设计上投入了更多的精力:-)这是他们达成共识的最佳解决方案.

Well they put considerably more effort in the design of extensions to the language today :-) It's the best solution that they could reach consensus for.

这篇关于JavaScript幂运算一元运算符的设计决策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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