JS中判断引用类型的方式主要有两种
通过instanceof
通过constructor
function Person() {}
var per = new Person();
console.log(per instanceof Person);//true
console.log(per.constructor == Person);//true
但是通过第二种方式判断会有一些问题,我们知道实例是通过proto指向构造函数的原型对象(prototype)来找到constructor。如果改写了构造函数的原型,或者改写了对象的constructor,都会让这种方法出错。
per.constructor = function(){
console.log('zz');
};
console.log(chinese.constructor == Person);//false
而 A instanceof B 是判断B是否在A的原型链上 ,所以推荐用 instanceof