What does = +_ mean in JavaScript

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

    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.

提交回复
热议问题