js 字符串,new String() 与 String()

Posted F

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了js 字符串,new String() 与 String()相关的知识,希望对你有一定的参考价值。

 

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\'));

// A. Case A
// B. Case B
// C. Do not know!
// D. undefined

 答案是C。

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(String(\'A\'));

// A. Case A
// B. Case B
// C. Do not know!
// D. undefined

答案是A。

在 switch 内部使用严格相等 === 进行判断,并且 new String("A") 返回的是一个对象,而 String("A") 则是直接返回字符串 "A"。

 

new String()生成的是一个字符串对象

 String生成的是一个字符串

 

 

以上是关于js 字符串,new String() 与 String()的主要内容,如果未能解决你的问题,请参考以下文章