标签:javascript
执行强制类型转换语句。
String
// bad <p>// => this.reviewScore = 9;</p> var A= this.reviewScore + ''; // good var totalScore = '' + this.reviewScore; // bad var totalScore = '' + this.reviewScore + ' total score'; // good var totalScore = this.reviewScore + ' total score';
var A= '4'; // bad var val = new Number(A); // bad var val = +A; // bad var val = A>> 0; // bad var val = parseInt(A); // good var val = Number(A); // good var val = parseInt(A, 10);
// good /** * parseInt was the reason my code was slow. * Bitshifting the String to coerce it to a * Number made it a lot faster. */ var val = inputValue >> 0;
2147483647 >> 0 //=> 2147483647 2147483648 >> 0 //=> -2147483648 2147483649 >> 0 //=> -2147483647Booleans:
var age = 0; // bad var hasAge = new Boolean(age); // good var hasAge = Boolean(age); // good var hasAge = !!age;
标签:javascript
原文地址:http://blog.csdn.net/princeterence/article/details/45843767