instanceof 就是判断是否在同一个原型链上来判断数据类型的
所以 instanceof 不能用来判断 number,string 等基本数据类型
function myInstanceof(foo, Fun) {
//判断的时候直接放入数组
const typeNums = ['number','string','boolean','undefined'];
//首先基本类型是不可以来判断的
if (!foo || !Fun || typeNums.includes(typeof foo) ) {
console.log('输入错误,无法判断');
return;
}
//先得到隐式原型和显示原型
let pro = Object.getPrototypeOf(foo);
const Proto = Fun.prototype
while (pro) {
if (pro === Proto) {
return true;
}
pro = Object.getPrototypeOf(pro);
}
return false;
}
console.log(myInstanceof([], Array));