JavaScript中的= + _是什么意思 [英] What does = +_ mean in JavaScript
问题描述
我想知道JavaScript中的 = + _ 运算符是什么意思。它看起来像是分配。
I was wondering what the = +_ operator means in JavaScript. It looks like it does assignments.
示例:
hexbin.radius = function(_) {
if (!arguments.length)
return r;
r = +_;
dx = r * 2 * Math.sin(Math.PI / 3);
dy = r * 1.5;
return hexbin;
};
推荐答案
r = +_;
-
+
尝试将任何_
投射到一个数字。 -
_
仅限于变量名(不是运算符),可以是a
,foo
等。 +
tries to cast whatever_
is to a number._
is only a variable name (not an operator), it could bea
,foo
etc.
示例:
+"1"
将1投射到纯数字1。
var _ = "1";
var r = +_;
r
现在 1
,而不是1
。
此外,根据算术运算符上的MDN页面:
一元加号运算符在其操作数之前,并计算为其
操作数,但尝试将其转换为数字,如果它不是
已经。 [...] 它可以转换整数和
浮点数的字符串表示形式,以及非字符串值true
,false
,null
。
十进制和十六进制(0x
-prefixed)格式的整数支持
。支持负数(但不支持十六进制)。如果
无法解析特定值,它将评估为NaN
。
The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already. [...] It can convert string representations of integers and floats, as well as the non-string values
true
,false
, andnull
. Integers in both decimal and hexadecimal ("0x"
-prefixed) formats are supported. Negative numbers are supported (though not for hex). If it cannot parse a particular value, it will evaluate toNaN
.
还注意到
一元加号是将某些东西转换为数字的最快和首选方式
unary plus is the fastest and preferred way of converting something into a number
这篇关于JavaScript中的= + _是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!