[英]JavaScript exponentiation unary operator design decision
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
From the documentation on MDN : 从MDN的文档 :
In JavaScript, it is impossible to write an ambiguous exponentiation expression, ie you cannot put a unary operator (
+
/-
/~
/!
/delete
/void
/typeof
) immediately before the base number.在JavaScript中,不可能编写一个模糊的取幂表达式,即你不能在基数之前放置一元运算符(
+
/-
/~
/!
/delete
/void
/typeof
)。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.在有一个指数操作符,如PHP和Python和其他大多数语言(通常
^
或**
)幂运算符被定义为具有比一元运算符,如一元一个更高的优先级+
和一元-
但也有一些例外。 For example, in Bash the**
operator is defined to have a lower precedence than unary operators.例如,在Bash中,
**
运算符被定义为具有比一元运算符更低的优先级。
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? 谁真的会感到惊讶
-x ** 2
是负面的? 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. 这不仅遵循其他主流编程语言,而且是数百年来常用的数学符号,并且教给每个高中代数学生。
In Javascript '1'+ 2
is '12'
and '1'-2
is -1
but -1**2
raises an error because it could be ambiguous? 在Javascript'1
'1'+ 2
是'12'
和'1'-2
是-1
但是-1**2
引发错误,因为它可能是不明确的? Help me understand this design decision. 帮助我理解这个设计决定。
I don't understand this design decision.
我不明白这个设计决定。
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 . 了解更多关于它在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 。
Who's really going to be surprised that
-x ** 2
is negative?谁真的会感到惊讶
-x ** 2
是负面的?
Enough people to matter. 足够重要的人。 Some relevant quotes from the above resources:
以上资源的一些相关引用:
**
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 **
比一元运算符更紧密地绑定会破坏x**-2
而使它有时更紧,有时更宽松会太混乱,并导致其他优先倒置的机会。 ” - Waldemar Horwat **
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 **
在其他语言中的历史之间存在冲突,[和]一元模式比二元更紧密的一般模式,此时的任何解决方案都会让许多人感到困惑。 ” - Mark S. Miller -x**2 === -(x ** 2)
and -x ** 2 === (-x) ** 2
" - Alexander Jones -x**2 === -(x ** 2)
和-x ** 2 === (-x) ** 2
” - Alexander Jones -
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 -
比**
更紧密。确实除了点(一个特殊形式,其右操作数必须是词汇标识符名称和方括号(本身不是中缀运算符),一元运算符绑定比JS中的二进制更紧密,就像C和其他C派生语言一样。 “ - Brendan Eich -5 2
. But for -5 ** 2
, because of the whitespace around the infix operator. Even without space, -
seems to be part of the literal. " - Dave Herman -5 2
但是, -5 ** 2
,因为周围管道符空白的即使没有空间。 -
似乎是字面的一部分。” -戴夫·赫尔曼 **
is the exponentiation operator. But people usually try to avoid dark corners so they never develop an intuition for negative bases. " - Dave Herman **
是指数运算符。但人们通常会试图避免黑暗角落,因此他们永远不会对负基础产生直觉“ -戴夫·赫尔曼 In Javascript
'1'+ 2
is'12'
and'1'-2
is-1
but-1**2
raises an error because it could be ambiguous?在Javascript'1
'1'+ 2
是'12'
和'1'-2
是-1
但是-1**2
引发错误,因为它可能是不明确的?
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. 那么他们在设计今天的语言扩展方面付出了相当多的努力:-)这是他们可以达成共识的最佳解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.