What does = +_ mean in JavaScript

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

提交回复
热议问题