问题描述
因此,我正与新的幂运算符鬼混,发现不能在基数前紧跟一元运算符.
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上的文档:
在大多数语言中,例如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.
推荐答案
有关详细信息,请访问 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 和 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.
足够重要的人.以上资源中的一些相关引用:
Enough people to matter. Some relevant quotes from the above resources:
- "使
**
绑定比一元运算符更紧密会破坏x**-2
.有时使其变得更紧密有时更松散会太混乱,并导致其他优先级倒置的机会."- Waldemar Horwat - "鉴于其他语言
**
的历史之间的冲突,以及[]一元绑定比二进制绑定更严格的一般模式,此时的任何解决方案都会使许多人感到困惑."- 马克·米勒 - "确认存在大量空白的可能性:
-x**2 === -(x ** 2)
和-x ** 2 === (-x) ** 2
"-亚历山大·琼斯 - "问题是,无论在取幂表达式之前出现一元负数是多少,都是因为缺少
-
比**
绑定更紧密的上标和较小的字体,所以实际上除了点(特殊形式,其右操作数必须是词法标识符名)和方括号(本身不是infix运算符),在C和其他C派生语言中,一元运算符在JS中的绑定比二进制更紧密.- Brendan Eich - "在数学上,显然
-5
.但是对于-5 ** 2
,由于infix运算符周围有空格.即使没有空格,-
似乎也是文字的一部分."-戴夫·赫尔曼(Dave Herman) - [关于编程语言的优先顺序],"实际上零人对其他语言有这种理解.同意人们对
**
是幂运算符的理解.但是人们通常会尽量避免出现黑角,因此永远不要为否定的基础发展直觉."-戴夫·赫尔曼
- "making
**
bind tighter than unary operators would breakx**-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
-5
. 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
他们今天在语言扩展设计上投入了更多的精力:-)这是他们达成共识的最佳解决方案.
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幂运算一元运算符的设计决策的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!