gpt4 book ai didi

javascript - = +_ 在 JavaScript 中是什么意思

转载 作者:IT老高 更新时间:2023-10-28 13:12:29 25 4
gpt4 key购买 nike

我想知道 = +_ 运算符在 JavaScript 中的含义。它看起来像是在做作业。

例子:

hexbin.radius = function(_) {
if (!arguments.length)
return r;
r = +_;
dx = r * 2 * Math.sin(Math.PI / 3);
dy = r * 1.5;
return hexbin;
};

最佳答案

r = +_;
  • + 尝试将任何 _ 转换为数字。
  • _ 只是一个变量名(不是运算符),它可以是 afoo 等。

示例:

+"1"

将“1”转换为纯数字 1。

var _ = "1";
var r = +_;

r 现在是 1,而不是 "1"

此外,根据 MDN page on Arithmetic Operators :

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, and null. 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 to NaN.

还要注意

unary plus is the fastest and preferred way of converting something into a number

关于javascript - = +_ 在 JavaScript 中是什么意思,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15129137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com