JavaScript 求幂一元运算符设计决策
JavaScript exponentiation unary operator design decision
所以我在玩弄新的求幂运算符,我发现你不能在基数之前立即放置一元运算符。
let result = -2 ** 2; // syntax error
let result = -(2 ** 2); // -4
let x = 3;
let result = --x ** 2; // 4
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.
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
为负数感到惊讶?这不仅遵循其他主流编程语言,而且遵循数百年来普遍使用的数学符号,并且教给每个高中代数学生。
在 Javascript 中 '1'+ 2
是 '12'
并且 '1'-2
是 -1
但是 -1**2
会引发错误,因为它可能有歧义?帮助我理解这个设计决策。
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-23.md#exponentiation-operator and 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
。并且有时更紧有时更松会太混乱并导致优先级反转的其他机会." - Waldemar Horwat
- "鉴于
**
在其他语言中的历史,[和] 一元比二进制绑定更紧密的一般模式之间的冲突,此时的任何解决方案都会让很多人感到困惑。 " - Mark S. Miller
- 承认显白的前景space:
-x**2 === -(x ** 2)
和-x ** 2 === (-x) ** 2
”-Alexander Jones
- "问题是,无论求幂表达式前的一元减号多么罕见,缺少带有较小字体的上标表明
-
比 [=11= 绑定更紧密]. 事实上,除了点(一种特殊形式,其右操作数必须是词法标识符名称)和方括号(本身不是中缀运算符)之外,一元运算符在 JS 中比在 C 和其他语言中的二元运算符绑定更紧密C 派生语言。" - Brendan Eich
- "对于数学来说
-5<sup>2</sup>
似乎很明显。但是对于 -5 ** 2
,因为中缀运算符周围的白色 space。即使没有 space,-
似乎也是文字的一部分。”- Dave Herman
- [关于编程语言的优先级],“实际上零个人从其他语言中获得了关于此的直觉。同意人们有一个直觉,即
**
是求幂运算符。但人们通常会尝试避免黑暗的角落,这样他们就永远不会对负碱基产生直觉。" - Dave Herman
In Javascript '1'+ 2
is '12'
and '1'-2
is -1
but -1**2
raises an error because it could be ambiguous?
好吧,他们今天在语言扩展的设计上付出了相当多的努力:-)这是他们可以达成共识的最佳解决方案。
所以我在玩弄新的求幂运算符,我发现你不能在基数之前立即放置一元运算符。
let result = -2 ** 2; // syntax error
let result = -(2 ** 2); // -4
let x = 3;
let result = --x ** 2; // 4
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.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
为负数感到惊讶?这不仅遵循其他主流编程语言,而且遵循数百年来普遍使用的数学符号,并且教给每个高中代数学生。
在 Javascript 中 '1'+ 2
是 '12'
并且 '1'-2
是 -1
但是 -1**2
会引发错误,因为它可能有歧义?帮助我理解这个设计决策。
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-23.md#exponentiation-operator and 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
。并且有时更紧有时更松会太混乱并导致优先级反转的其他机会." - Waldemar Horwat - "鉴于
**
在其他语言中的历史,[和] 一元比二进制绑定更紧密的一般模式之间的冲突,此时的任何解决方案都会让很多人感到困惑。 " - Mark S. Miller - 承认显白的前景space:
-x**2 === -(x ** 2)
和-x ** 2 === (-x) ** 2
”-Alexander Jones - "问题是,无论求幂表达式前的一元减号多么罕见,缺少带有较小字体的上标表明
-
比 [=11= 绑定更紧密]. 事实上,除了点(一种特殊形式,其右操作数必须是词法标识符名称)和方括号(本身不是中缀运算符)之外,一元运算符在 JS 中比在 C 和其他语言中的二元运算符绑定更紧密C 派生语言。" - Brendan Eich - "对于数学来说
-5<sup>2</sup>
似乎很明显。但是对于-5 ** 2
,因为中缀运算符周围的白色 space。即使没有 space,-
似乎也是文字的一部分。”- Dave Herman - [关于编程语言的优先级],“实际上零个人从其他语言中获得了关于此的直觉。同意人们有一个直觉,即
**
是求幂运算符。但人们通常会尝试避免黑暗的角落,这样他们就永远不会对负碱基产生直觉。" - Dave Herman
In Javascript
'1'+ 2
is'12'
and'1'-2
is-1
but-1**2
raises an error because it could be ambiguous?
好吧,他们今天在语言扩展的设计上付出了相当多的努力:-)这是他们可以达成共识的最佳解决方案。