-5
<script>
var pimitiveString1 = "This is a primitive string";    
var pimitiveString2 = String("This is a primitive string");    
alert(typeof primitiveString1);
alert(typeof primitiveString2);  
</script>

问题:

它显示“未定义”,为什么它不是“字符串”?pimitiveString1 和 pimitiveString2 是字符串类型,对吧?

4

3 回答 3

2

拼写错误

var primitiveString1 = "This is a primitive string";    
var primitiveString2 = String("This is a primitive string");    
alert(typeof primitiveString1);
alert(typeof primitiveString2);  
于 2013-06-17T01:09:56.393 回答
1

那是因为错字:

pimitiveString1v/sprimitiveString1

alert(typeof pimitiveString1); //This will alert string
alert(typeof pimitiveString2);  // This will be a string
于 2013-06-17T01:10:06.993 回答
1

String错误地调用了构造函数。正确的方法是

<script>
var primitiveString1 = "This is a primitive string";    
var primitiveString2 = new String("This is a primitive string");    
alert(typeof primitiveString1);
alert(typeof primitiveString2);  
</script>

您的标识符中还有一个typo。它应该var primitiveString1代替var pimitiveString1

于 2013-06-17T01:10:17.697 回答