日志文章

2008年07月01日 20:59:00

typeof 和instanceof的用法和区别

(1) typeof 有一个参数,可以是变量或值
  例如:
  var str ="Hello world" ;
  alert( typeof str) ; // return "string"
  alert ( typeof 65); // retrun "number"
  对于变量或值 typeof 将返回下列值之一
  "undefined "   如果变量是Undefined类型
  "boolean"     如果变量是Boolean类型
  "number"     如果变量是Number类型
  "string"       如果变量是String类型
  "object"       如果变量是引用或Null类型
(2)instanceof 与typeof 的区别在与对于引用类型
  typeof 都返回"object" ,而instanceof 能返回具体的类型
  例如
    var str ="test string"
    if (str instanceof string)
      {
      alert(" is string");
      }

Tags: typeof   instanceof  

类别: javascript |  评论(0) |  浏览(784) |  收藏
发表评论