Is Subtracting Zero some sort of JavaScript performance trick?

前端 未结 7 1203
一向
一向 2020-12-06 00:32

Looking in the jQuery core I found the following code convention:

nth: function(elem, i, match){
    return match[3] - 0 === i;
},

And I wa

7条回答
  •  北荒
    北荒 (楼主)
    2020-12-06 01:04

    Various ways to coerse JS strings to numbers, and their consequences:


    (source: phrogz.net)

    I personally use *1 as it is short to type, but still stands out (unlike the unary +), and either gives me what the user typed or fails completely. I only use parseInt() when I know that there will be non-numeric content at the end to ignore, or when I need to parse a non-base-10 string.

提交回复
热议问题