zoukankan      html  css  js  c++  java
  • javascript中 typeof和instanceof的区别

    <一> js中typeof的用法进行了详细的汇总介绍

    (1)返回一个变量的基本类型

      回顾基本类型(number,string,boolean,null,undefined,object)

        console.log(typeof 1); // number
        console.log(typeof 'abc'); // string
        console.log(typeof true); // boolean
        let o =  {a:'23'};
        console.log(typeof o); // object
        console.log(typeof null); // object  
        console.log(typeof undefined); // undefined

    备注1: null表示 “没有对象”,即该处不应该有值;undefined表示“缺少值”,就是此处应该有值,但是没有定义。

    备注2:判断一个值既不是null,也不是undefeated,

    问题:如果你想检测一个值是否被定义过(值不是undefined也不是null),那么你就遇到了typeof最有名的一个怪异表现(被认为是一个bug):typeof null返回了"object":

        function isDefined(x) {
            return x!== null && x!== undefined;
        }

    备注2:typeof null 是object,是不是bug,从另一个角度看,如果null是一个真正意义上对象的话,它应该放在与array,function同级别的位置,而不是跟undefined,string同级别,但是typeof null返回object,矛盾。

    链接:http://www.ruanyifeng.com/blog/2014/03/undefined-vs-null.html

    (2)typeof 检查一个变量是否存在,是否有值

        if(typeof o === 'object'){
            console.log("o是一个对象")
        }else {
            console.log("o不是一个对象")
        }

    备注:同理,别的基本类型也可以采用相同的办法。

    <二> js中的instanceof运算符

        主要功能:检测应用类型,是数组,正则等

    (1)instanceof的普通用法, obj instanceof Object 检测Object.prototype是否存在于参数obj的原型链上

      Person的原型在p原型链中

        function Person(){};
        var p =new Person();
        console.log(p instanceof Person);//true

    (2)继承中判断实例是否属于它的父类   

      Student和Person都在s的原型链中

        function Person(){};
        function Student(){};
        var p =new Person();
        Student.prototype=p;//继承原型
        var s=new Student();
        console.log(s instanceof Student);//true
        console.log(s instanceof Person);//true

     备注:instanceof只能用来判断对象和函数,不能用来判断字符串和数字

    (3)复杂用法

    function Person() {}
    console.log(Object instanceof Object);     //true
    //第一个Object的原型链:Object=>
    //Object.__proto__ => Function.prototype=>Function.prototype.__proto__=>Object.prototype
    //第二个Object的原型:Object=> Object.prototype
    
    console.log(Function instanceof Function); //true
    //第一个Function的原型链:Function=>Function.__proto__ => Function.prototype
    //第二个Function的原型:Function=>Function.prototype
    
    console.log(Function instanceof Object);   //true
    //Function=>
    //Function.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
    //Object => Object.prototype
    
    console.log(Person instanceof Function);      //true
    //Person=>Person.__proto__=>Function.prototype
    //Function=>Function.prototype
    
    console.log(String instanceof String);   //false
    //第一个String的原型链:String=>
    //String.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
    //第二个String的原型链:String=>String.prototype
    
    console.log(Boolean instanceof Boolean); //false
    //第一个Boolean的原型链:Boolean=>
    //Boolean.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
    //第二个Boolean的原型链:Boolean=>Boolean.prototype
    
    console.log(Person instanceof Person); //false
    //第一个Person的原型链:Person=>
    //Person.__proto__=>Function.prototype=>Function.prototype.__proto__=>Object.prototype
    //第二个Person的原型链:Person=>Person.prototype
    

     (4)总结 

    function _instanceof(A, B) {
        var O = B.prototype;// 取B的显示原型
        A = A.__proto__;// 取A的隐式原型
        while (true) {
            //Object.prototype.__proto__ === null
            if (A === null)
                return false;
            if (O === A)// 这里重点:当 O 严格等于 A 时,返回 true
                return true;
            A = A.__proto__;
        }
    }

    <三> typeof 与instanceof区别

       typeof和instanceof的目的都是检测变量的类型,两个的区别在于typeof一般是检测的是基本数据类型,instanceof主要检测的是引用类型!

  • 相关阅读:
    bzoj2064分裂(dp)
    关于逆元的学习笔记(尚未完成)
    线性基学习笔记
    bzoj2460元素(线性基,贪心)
    CF280C Game on tree(期望dp)
    XJOI contest800
    关闭极域电子教室学生端的各种方法
    大坑!有网,电脑qq登不上去!!
    4.25 ZJOI2017 10AM
    bzoj1621
  • 原文地址:https://www.cnblogs.com/yyy6/p/6916156.html
Copyright © 2011-2022 走看看