美文网首页Web前端之路让前端飞
JS String 和 new String的区别

JS String 和 new String的区别

作者: Tiny_z | 来源:发表于2016-12-14 10:58 被阅读134次

之前看到一个题目如下,因为基础知识不扎实,看起来很简单的一个流程语句,都没有答上来,特此记录下

function showCase(value) {
   switch(value) {
   case 'A':
       console.log('Case A');
       break;
   case 'B':
       console.log('Case B');
       break;
   case undefined:
       console.log('undefined');
       break;
   default:
       console.log('Do not know!');
   }
}
showCase(new String('A'));//Do not know

当通过new 调用 String()时,返回的是一个object



当通过字面量或者直接调用String(),返回的是一个字符串



并且在switch中,都是严格比较。所以上面 new String('A') === 'A' 为false

相关文章

网友评论

    本文标题:JS String 和 new String的区别

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