zoukankan      html  css  js  c++  java
  • typeof instanceof 之间的区别总结

        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
     
  • 相关阅读:
    新的学期,新的学习
    织梦最新
    selectd选项跳转链接
    字体统一大小
    js点击后进入到另一个页面的TAB选项中
    织梦套后台
    js获取距离各边的距离
    将时间的秒数转化为分钟数
    jquery手写tab选项卡功能
    js中的定时器
  • 原文地址:https://www.cnblogs.com/william-lin/p/5721007.html
Copyright © 2011-2022 走看看