天使半只翼

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
    typeof
  它返回值是一个字符串,该字符串说明运算数的类型。
    a=1;
    b=true;
    c="c";
    d=function(){
        console.log(" is d");
    }
    e={ e1:"is e1"}
    f=null;
    g=[1,2,3];
     
    console.log("a typeof="+typeof(a));
    console.log("b typeof="+typeof(b));
    console.log("c typeof="+typeof(c));
    console.log("d typeof="+typeof(d));
    console.log("e typeof="+typeof(e));
    console.log("f typeof="+typeof(f));
得到的结果:
 
了解到 typeof  一般只能返回如下几个结果:number,boolean,string,function,object,undefined。
Null,Array返回的也是object;
null值表示一个空对象指针,而这正是使用typeof操作符检测null值时会返回“object”的原因。
 

instanceof

instance:实例,例子
instanceof 用于判断一个变量是否某个对象的实例
    console.log(null instanceof Object);
    console.log(Array instanceof Object);

得到结果:

 
可以了解到Array是Object的子类,所以上面的程序 g=[1,2,3]; 返回的是 object
 
posted on 2016-07-30 15:18  天使半只翼  阅读(302)  评论(0编辑  收藏  举报