我想知道= +_操作符在JavaScript中是什么意思。它看起来像是在做作业。

例子:

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

当前回答

简单地说,+_相当于使用Number()构造函数。

事实上,它甚至适用于约会:

var d = new Date('03/27/2014');
console.log(Number(d)) // returns 1395903600000
console.log(+d) // returns 1395903600000

演示: http://jsfiddle.net/dirtyd77/GCLjd/


更多信息也可以在MDN - Unary plus(+)部分找到:

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. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. 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.

其他回答

它将给左边变量一个数字赋新值。

var a=10;
var b="asg";
var c=+a;//return 10
var d=-a;//return -10
var f="10";

var e=+b;
var g=-f;

console.log(e);//NAN
console.log(g);//-10

_只是一个变量名,作为函数hexbin的参数传递。半径,并将其转换为数字

让我举一个和你的函数一样的例子。

var hexbin = {},r  ;

hexbin.radius = function(_) {
   if (!arguments.length)
      return r;
   console.log( _ , typeof _ )    
   r = +_;
   console.log( r , typeof r , isNaN(r) );   
}

并运行这个示例函数..的输出

hexbin.radius( "1");

1 string
1 number false 

hexbin.radius( 1 );

1 number
1 number false

hexbin。半径([]);

[] object
0 number false

hexbin。半径('a');

a string
NaN number true

hexbin。半径({});

Object {} object
NaN number true

hexbin。半径(真);

true boolean
1 number false

简单地说,+_相当于使用Number()构造函数。

事实上,它甚至适用于约会:

var d = new Date('03/27/2014');
console.log(Number(d)) // returns 1395903600000
console.log(+d) // returns 1395903600000

演示: http://jsfiddle.net/dirtyd77/GCLjd/


更多信息也可以在MDN - Unary plus(+)部分找到:

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. Although unary negation (-) also can convert non-numbers, unary plus is the fastest and preferred way of converting something into a number, because it does not perform any other operations on the number. 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.

这是一个鬼鬼祟祟的问题。

需要理解的重要一点是,这里的下划线字符实际上是一个变量名,而不是一个操作符。

它前面的加号是得到下划线的正数值——即有效地将下划线变量转换为int型。您可以使用parseInt()实现相同的效果,但这里可能使用加号强制转换,因为它更简洁。

等号只是一个标准的变量赋值。

它可能不是故意写来混淆视听的,因为有经验的Javascript程序员通常会将下划线识别为变量。但如果你不知道,那肯定会很困惑。我当然不会这样写;在最好的情况下,我不喜欢使用没有意义的短变量名——如果你想在JS代码中使用短变量名来节省空间,请使用minifier;不要一开始就写短变量。

它不是赋值操作符。

_只是传递给函数的参数。 hexbin。半径=函数_ { // ^它被传递到这里 / /…… }; 下一行r = +_;+ infront将该变量(_)转换为一个数字或整数值,并将其赋给变量r

不要把它与+=运算符混淆