美文网首页
js 类型的转换

js 类型的转换

作者: 许道龙 | 来源:发表于2016-07-15 09:10 被阅读0次

    转成字符串

    // good
    const totalScore = String(this.reviewScore);
    

    转成数字

    const inputValue = '4';
    
    // bad
    const val = new Number(inputValue);
    
    // bad
    const val = +inputValue;
    
    // bad
    const val = inputValue >> 0;
    
    // bad
    const val = parseInt(inputValue);
    
    // good
    const val = Number(inputValue);
    
    // good
    const val = parseInt(inputValue, 10);
    

    转成布尔

    const age = 0;
    
    // bad
    const hasAge = new Boolean(age);
    
    // good
    const hasAge = Boolean(age);
    
    // good
    const hasAge = !!age;
    

    相关文章

      网友评论

          本文标题:js 类型的转换

          本文链接:https://www.haomeiwen.com/subject/illyjttx.html