= +_ 在 JavaScript 中是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15129137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 23:37:03  来源:igfitidea点击:

What does = +_ mean in JavaScript

javascriptoperators

提问by Dimitry

I was wondering what the = +_operator means in JavaScript. It looks like it does assignments.

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

Example:

例子:

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

回答by mpm

r = +_;
  • +tries to cast whatever _is to a number.
  • _is only a variable name (not an operator), it could be a, fooetc.
  • +尝试将任何_内容转换为数字。
  • _只是一个变量名(不是一个运算符),它可以是afoo等等。

Example:

例子:

+"1"

cast "1" to pure number 1.

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

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

ris now 1, not "1".

r是现在1,不是"1"

Moreover, according to the MDN page on Arithmetic Operators:

此外,根据关于算术运算符MDN 页面

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.

一元加号运算符位于其操作数之前并计算其操作数,但尝试将其转换为数字(如果尚未转换为数字)[...]它可以转换整数和浮点数的字符串表示,以及非字符串值truefalsenull。支持十进制和十六进制(-"0x"前缀)格式的整数。支持负数(尽管不支持十六进制)。如果它无法解析特定值,它将评估为NaN

It is also noted that

还应注意的是

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

一元加号是将某物转换为数字的最快和首选方式

回答by Starx

It is not an assignment operator.

它不是赋值运算符。

  • _is just a parameter passed to the function.

    hexbin.radius = function(_) {
                    //       ^ It is passed here
        // ...
    };
    
  • On the next line r = +_;+infront casts that variable (_) to a number or integer value and assigns it to variable r

  • _只是传递给函数的参数。

    hexbin.radius = function(_) {
                    //       ^ It is passed here
        // ...
    };
    
  • 在下一行r = +_;+infront 将该变量 ( _)强制转换为数字或整数值并将其分配给变量r

DO NOT CONFUSE IT WITH +=operator

不要将它与+=操作员混淆

回答by Grijesh Chauhan

=+are actually twooperators =is assignment and +and _is variable name.

=+实际上有两个运算符=是赋值+_是变量名。

like:

喜欢:

i = + 5;
or 
j = + i;
or 
i = + _;

My following codes will help you to show use of =+to convert a stringinto int.
example:

我的以下代码将帮助您展示=+字符串转换为int 的用法。
例子:

y = +'5'
x = y +5
alert(x);

outputs 10

输出10

use:So here yis int 5because of =+
otherwise:

使用:所以这里y是 int5因为=+
否则:

y = '5'
x = y +5
alert(x);

outputs 55

输出55

Where as _is a variable.

其中 as_是一个变量。

_ = + '5'
x = _ + 5
alert(x)

outputs 10

输出10

Additionally,It would be interesting to know you could also achieve same thing with ~(if string is intstring (float will be round of to int))

此外,知道你也可以用~(如果字符串是int字符串(浮点数将舍入为整数)实现同样的事情会很有趣

y = ~~'5'  // notice used two time ~
x = y  + 5
alert(x);

also outputs 10

也输出10

~is bitwise NOT : Inverts the bits of its operand. I did twice for no change in magnitude.

~按位非:反转其操作数的位。我做了两次,幅度没有变化。

回答by Ovilia

It's not =+. In JavaScript, +means change it into number.

不是=+。在 JavaScript 中,+意味着将其转换为数字。

+'32'returns 32.

+'32'返回 32。

+'a'returns NaN.

+'a'返回 NaN。

So you may use isNaN()to check if it can be changed into number.

所以你可以isNaN()用来检查它是否可以变成数字。

回答by SDC

It's a sneaky one.

这是一个偷偷摸摸的。

The important thing to understand is that the underscore character here is actually a variable name, not an operator.

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

The plus sign in front of that is getting the positive numerical value of underscore -- ie effectively casting the underscore variable to be an int. You could achieve the same effect with parseInt(), but the plus sign casting is likely used here because it's more concise.

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

And that just leaves the equals sign as just a standard variable assignment.

这只是将等号作为标准变量赋值。

It's probably not deliberatelywritten to confuse, as an experienced Javascript programmer will generally recognise underscore as a variable. But if you don't know that it is definitely very confusing. I certainly wouldn't write it like that; I'm not a fan of short meaningless variable names at the best of times -- If you want short variable names in JS code to save space, use a minifier; don't write it with short variables to start with.

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

回答by Harsha Ivaturi

= +_ will cast _ into a number.

= +_ 会将 _ 转换为数字。

So

所以

var _ = "1",
   r = +_;
console.log(typeof r)

would output number.

会输出数字。

回答by KooiInc

I suppose you mean r = +_;? In that case, it's conversion of the parameter to a Number. Say _is '12.3', then +'12.3'returns 12.3. So in the quoted statement +_is assigned to r.

我想你的意思是r = +_;?在这种情况下,它将参数转换为Number. 说_是'12.3',然后+'12.3'返回12.3。所以在引用语句中+_被赋值为r.

回答by rab

_is just a a variable name, passed as a parameter of function hexbin.radius, and +cast it into number

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

Let me make a exmple same like your function .

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

var hexbin = {},r  ;

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

and run this example function .. which outputs

并运行此示例函数 .. 输出

hexbin.radius( "1");

hexbin.radius("1");

1 string
1 number false 

hexbin.radius( 1 );

hexbin.radius(1);

1 number
1 number false

hexbin.radius( [] );

hexbin.radius([]);

[] object
0 number false

hexbin.radius( 'a' );

hexbin.radius('a');

a string
NaN number true

hexbin.radius( {} );

hexbin.radius({});

Object {} object
NaN number true

hexbin.radius( true );

hexbin.radius(真);

true boolean
1 number false

回答by Amrendra

It Will assign new value to left side variable a number.

它将为左侧变量分配新值一个数字。

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

回答by Brian

+_is almost equivalentof parseFloat(_). Observe that parseIntwill stop at non numeric character such as dot, whereas parshFloatwill not.

+_几乎是等同parseFloat(_)。观察到parseInt会在非数字字符处停止,例如点,而parshFloat不会。

EXP:

经验:

    parseFloat(2.4) = 2.4 
vs 
    parseInt(2.4) = 2 
vs 
    +"2.4" = 2.4

Exp:

经验:

var _ = "3";
    _ = +_;

console.log(_); // will show an integer 3

Very few differences:

很少有区别: