xiaozi 2015-01-05 23:16:45
js中,将数字字符串转换成数字,parseInt不是很好的办法,因为当你忘了传第二个参数的时候,会出现一些问题
~~"42" //=> 42
~~"042" //=> 42
~~"the 42" //=> 0 <- Advantage of using ~~
~~"42px" //=> 0
+"42" //=> 42
+"042" //=> 42
+"the 42" //=> NaN
+"42px" //=> NaN
parseInt("42") //=> 42
parseInt("042") //=> 34 (Firefox) || 42 (Chrome)
parseInt("09") //=> NaN (Firefox) || 9 (Chrome)
parseInt("the 42") //=> NaN
parseInt("42px") //=> 42 <- Advantage of using parseInt