何时使用哪个
这条线有什么区别:
var a = parseInt("1", 10); // a === 1
和这条线
var a = +"1"; // a === 1
这个jsperf测试表明,一元运算符在当前chrome版本中快得多,假设它是为node.js !?
  如果我尝试转换不是数字的字符串都返回NaN : 
var b = parseInt("test" 10); // b === NaN
var b = +"test"; // b === NaN
  那么,我应该什么时候更喜欢使用parseInt加上一元加号(特别是在node.js中)? 
  编辑 :和双重代字符运算符~~什么区别? 
请查看此答案以获取更完整的一组案例
那么,我知道一些差异:
  一个空字符串""评估为0 ,而parseInt评估它为NaN 。  海事组织,一个空白的字符串应该是NaN 。 
+'' === 0;              //true
isNaN(parseInt('',10)); //true
  一元+行为更像parseFloat因为它也接受小数。 
  另一方面, parseInt在看到一个非数字字符时停止解析,例如打算成为小数点的时间段.  。 
+'2.3' === 2.3;           //true
parseInt('2.3',10) === 2; //true
  parseInt和parseFloat解析并生成从左到右的字符串。  如果他们看到一个无效字符,它会将已解析(如果有)作为数字返回,如果没有解析为数字,则返回NaN 。 
  如果整个字符串不可转换为数字,则一元组+将返回NaN 。 
parseInt('2a',10) === 2; //true
parseFloat('2a') === 2;  //true
isNan(+'2a');            //true
  正如@Alex K.的评论所见, parseInt和parseFloat将按字符进行解析。  这意味着十六进制和指数符号将失败,因为x和e被视为非数字组件(至少在base10上)。 
  一元+将正确地转换它们。 
parseInt('2e3',10) === 2;  //true. This is supposed to be 2000
+'2e3' === 2000;           //true. This one's correct.
parseInt("0xf", 10) === 0; //true. This is supposed to be 15
+'0xf' === 15;             //true. This one's correct.
  最终的任意数字转换表: 
EXPRS = [
    'parseInt(x)',
    'parseFloat(x)',
    'Number(x)',
    '+x',
    '~~x',
    'x>>>0',
    'isNaN(x)'
];
VALUES = [
    '"123"',
    '"+123"',
    '"-123"',
    '"123.45"',
    '"-123.45"',
    '"12e5"',
    '"12e-5"',
    
    '"0123"',
    '"0000123"',
    '"0b111"',
    '"0o10"',
    '"0xBABE"',
    
    '"4294967295"',
    '"123456789012345678"',
    '"12e999"',
    '""',
    '"123foo"',
    '"123.45foo"',
    '"  123   "',
    '"foo"',
    '"12e"',
    '"0b567"',
    '"0o999"',
    '"0xFUZZ"',
    '"+0"',
    '"-0"',
    '"Infinity"',
    '"+Infinity"',
    '"-Infinity"',
    'null',
    '[].undef',
    'true',
    'false',
    'Infinity',
    'NaN',
    '{}',
    '{valueOf: function(){return 42}}',
    '{toString: function(){return "56"}}',
];
//////
function wrap(tag, s) {
    if (s && s.join)
        s = s.join('');
    return '<' + tag + '>' + String(s) + '</' + tag + '>';
}
function table(head, rows) {
    return wrap('table', [
        wrap('thead', tr(head)),
        wrap('tbody', rows.map(tr))
    ]);
}
function tr(row) {
    return wrap('tr', row.map(function (s) {
        return wrap('td', s)
    }));
}
function val(n) {
    return n === true || Number.isNaN(n) ? wrap('b', n) : String(n);
}
var rows = VALUES.map(function (v) {
    var x = eval('(' + v + ')');
    return [v].concat(EXPRS.map(function (e) {
        return val(eval(e))
    }));
});
document.body.innerHTML = table(["x"].concat(EXPRS), rows);table { border-collapse: collapse }
tr:nth-child(odd) { background: #fafafa }
td { border: 1px solid #e0e0e0; padding: 5px; font: 12px monospace }
td:not(:first-child) { text-align: right }
thead td { background: #3663AE; color: white }
b { color: red }thg435的答案中的表格我认为是全面的,但我们可以总结出以下模式:
true为1,但"true"到NaN 。 parseInt更为宽松。  parseInt('123abc') === 123 ,而+报告NaN 。 Number将接受有效的十进制数字,而parseInt只会丢弃小数点后的所有内容。  因此, parseInt模仿C行为,但可能不是评估用户输入的理想选择。 parseInt是一个设计错误的解析器,它接受八进制和十六进制输入。  一元加只需要十六进制。   Falsy值将转换为Number后面的内容在C中有意义: null和false都为零。  "" 0不完全遵循这个约定,但对我来说意义重大。 
因此,我认为如果你正在验证用户输入,一元加法除了接受小数点之外,对其他所有方面都有正确的行为(但在我的真实生活中,我更感兴趣的是抓取电子邮件输入而不是userId,完全省略值等),而parseInt太自由了。
链接地址: http://www.djcxy.com/p/12833.html上一篇: when to use which
