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。spa

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。code

在 switch 內部使用嚴格相等 === 進行判斷,而且 new String("A") 返回的是一個對象,而 String("A") 則是直接返回字符串 "A"。對象

 

new String()生成的是一個字符串對象blog

 String生成的是一個字符串字符串

 

相關文章
相關標籤/搜索