What does = +_ mean in JavaScript

前端 未结 12 1728
南笙
南笙 2020-11-22 11:37

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

Example:

hexbin.radius = function(_)         


        
相关标签:
12条回答
  • 2020-11-22 12:32

    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
    
    0 讨论(0)
  • 2020-11-22 12:34

    +_ is almost equivalent of parseFloat(_) . Observe that parseInt will stop at non numeric character such as dot, whereas parshFloat will not.

    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:

    • Empty string "" evaluates to a 0, while parseInt() evaluates to NaN
    • For more info look here: parseInt vs unary plus - when to use which
    0 讨论(0)
  • 2020-11-22 12:35
    r = +_;
    
    • + tries to cast whatever _ is to a number.
    • _ is only a variable name (not an operator), it could be a, foo etc.

    Example:

    +"1"
    

    cast "1" to pure number 1.

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

    r is now 1, not "1".

    Moreover, according to the 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.

    It is also noted that

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

    0 讨论(0)
  • 2020-11-22 12:37

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

    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");

    1 string
    1 number false 
    

    hexbin.radius( 1 );

    1 number
    1 number false
    

    hexbin.radius( [] );

    [] object
    0 number false
    

    hexbin.radius( 'a' );

    a string
    NaN number true
    

    hexbin.radius( {} );

    Object {} object
    NaN number true
    

    hexbin.radius( true );

    true boolean
    1 number false
    
    0 讨论(0)
  • 2020-11-22 12:37

    Simply put, +_ is equivalent to using the Number() constructor.

    In fact, it even works on dates:

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

    DEMO: http://jsfiddle.net/dirtyd77/GCLjd/


    More information can also be found on MDN - Unary plus (+) section:

    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.

    0 讨论(0)
  • 2020-11-22 12:44

    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.

    0 讨论(0)
提交回复
热议问题