What does = +_ mean in JavaScript

前端 未结 12 1727
南笙
南笙 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:18

    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

    DO NOT CONFUSE IT WITH += operator

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

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

    +'32' returns 32.

    +'a' returns NaN.

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

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

    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.

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

    It's probably not deliberately written 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.

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

    = +_ will cast _ into a number.

    So

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

    would output number.

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

    In this expression:

    r = +_;
    
    • '+' acts here as an unary operator that tries to convert the value of the right operand. It doesn't convert the operand but the evaluated value. So _ will stay "1" if it was so originally but the r will become pure number.

    Consider these cases whether one wants to apply the + for numeric conversion

    +"-0" // 0, not -0
    +"1" //1
    +"-1" // -1
    +"" // 0, in JS "" is converted to 0
    +null // 0, in JS null is converted to 0
    +undefined // NaN
    +"yack!" // NaN
    +"NaN" //NaN
    +"3.14" // 3.14
    
    var _ = "1"; +_;_ // "1"
    var _ = "1"; +_;!!_ //true
    var _ = "0"; +_;!!_ //true
    var _ = null; +_;!!_ //false
    

    Though, it's the fastest numeric converter I'd hardly recommend one to overuse it if make use of at all. parseInt/parseFloat are good more readable alternatives.

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

    =+ are actually two operators = 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 string into int.
    example:

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

    outputs 10

    use: So here y is int 5 because of =+
    otherwise:

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

    outputs 55

    Where as _ is a variable.

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

    outputs 10

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

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

    also outputs 10

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

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