为什么typeof字符串显示' undefined'在js

时间:2013-06-17 01:07:14

标签: javascript

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

问题:

它显示'undefined',为什么它不是'string'? pimitiveString1和pimitiveString2是字符串类型,对吧?

3 个答案:

答案 0 :(得分:2)

拼写错误

var primitiveString1 = "This is a primitive string";    
var primitiveString2 = String("This is a primitive string");    
alert(typeof primitiveString1);
alert(typeof primitiveString2);  

答案 1 :(得分:1)

那是因为拼写错误:

pimitiveString1 v / s primitiveString1

alert(typeof pimitiveString1); //This will alert string
alert(typeof pimitiveString2);  // This will be a string

答案 2 :(得分: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