typeof和instanceof运算符返回相同var的不同信息

时间:2013-10-26 14:33:51

标签: javascript

观察以下JavaScript代码行:

<!DOCTYPE html>
<html>
   <head>
      <title>typeof vs instanceof</title>
   </head>
   <body>
      <script type="text/javascript">

var myString = "MyString";
alert( typeof myString );
alert( myString instanceof String );

      </script>
   </body>
</html>

当第二个提示string时,第一个提醒说false(小写)...

为什么?

1 个答案:

答案 0 :(得分:1)

您将原语类型string与您的对象的类型对象混淆,后者是String的一个实例。他们是不同的。

var s = "a"; // a string, typeof is "string"
var s = new String("a"); // an instance of String, typeof is "object"
相关问题