一元加减运算符的重要用途是什么? [英] What's the significant use of unary plus and minus operators?
问题描述
如果一元 +
/ -
运算符用于执行转换为 Number( )
转换函数,那为什么我们需要一元运算符?这些一元运营商的特殊需求是什么?
If unary +
/-
operators are used to perform conversions as the Number()
casting function, then why do we need unary operators? What's the special need of these unary operators?
推荐答案
一元 +
运算符将其操作数转换为数字类型。
一元 -
运算符将其操作数转换为数字类型,然后将其否定。
(根据 ECMAScript规范)
The Unary +
operator converts its operand to Number type.
The Unary -
operator converts its operand to Number type, and then negates it.
(per the ECMAScript spec)
实际上,一元 -
用于简单地将负数放在正常表达式中,例如:
In practice, Unary -
is used for simply putting negative numbers in normal expressions, e.g.:
var x = y * -2.0;
这是工作中的一元减号运算符。一元 +
等同于作为函数调用的Number()构造函数,如规范所暗示的那样。
That's the unary minus operator at work. The Unary +
is equivalent to the Number() constructor called as a function, as implied by the spec.
我只能推测历史,但是一元+/-运算符在许多C派生语言中表现相似。我怀疑Number()行为是这里语言的补充。
I can only speculate on the history, but the unary +/- operators behave similarly in many C-derived languages. I suspect the Number() behavior is the addition to the language here.
这篇关于一元加减运算符的重要用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!