查看 jQuery 核心,我发现了以下代码约定:
nth: function(elem, i, match){
return match[3] - 0 === i;
},
我真的对片段 match[3] - 0
在 Google 上搜索“-0”的效率并不高,搜索“负零”会返回对鲍勃·迪伦 (Bob Dylan) 歌曲的引用。
所以,谁能告诉我。这是某种性能技巧,还是有这样做的原因而不是 parseInt
或 parseFloat
?
最佳答案
可能只是强制左侧为整数的一种简写方法。当然,不如调用函数那么清晰。
This tutorial on type-conversion状态:
Any mathematical operator except the concatenation/addition operator will force type-conversion. So conversion of a string to a number might entail performing a mathematical operation on the string representation of the number that would not affect the resulting number, such as subtracting zero or multiplying by one.
这也表明“减去”是比“减”更好的搜索词。 :)
关于javascript - 减零是某种 JavaScript 性能技巧吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2665984/