js中的instanceof 和 typeof

typeof:

typeof 是大家经常用到的方法:

        var a = {
            "name":'suonidfahao',
            "job": 'webEnginner'
        }   
        var b = [1,2,4,4];
        var c;
        var d = null;

        console.log( typeof "123" );//string
        console.log(typeof 123);//number
        console.log(typeof a);//object
        console.log(typeof b);//object
        console.log(typeof true);//boolean
        console.log(typeof c);//undefined
        console.log(typeof d);//object
        console.log(typeof e);//undefined

值得注意的是,是typeof array 依然是object(见上typeof b)
关于array的类型判断,请见http://blog.csdn.net/qq_33619285/article/details/70144926


instanceof

instanceof运算符用来判断一个构造函数的prototype属性所指向的对象是否存在另外一个要检测对象的原型链上

        var Cat = function(){
            this.name = "maomi";
            this.callName = function(){
                console.log(this.name);
            }
        }
        var cat1 = new Cat();
        console.log(cat1 instanceof Cat);//true
        //because cat1.__proto__ == Cat.prototype // true

由此,也有

        var a = {
            "name": 'suonidfahao',
            "job": 'webEnginner'
        }   

        console.log(a instanceof Object)//true

回到说typeof 中说道 typeof array还是object的问题,在instanceof里就可以判断array了,

        var a = {
            "name":'suonidfahao',
            "job": 'webEnginner',
        }   
        console.log(a instanceof Object); //true
        console.log(a instanceof Array); //false
        var b = [1,2,4,4];
        console.log(b instanceof Object); //true 
        console.log(b instanceof Array); //true

深入讲解 instanceof

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

        function Person(){
            this.type = "human";
        };
        function Student(){
            this.job = "gotoschool";
        };
        var p = new Person();
        Student.prototype=p;
        Student.prototype.constructor = Student;
        var s = new Student();
        console.log(s instanceof Student);//true
        console.log(s instanceof Person);//true

深入Object/Function原型链

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

自行定义instanceof

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__;
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33619285/article/details/70142527