What does = +_ mean in JavaScript

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

提交回复
热议问题