##基本描述
- 内部作用机制,是通过对象中的原型链中是不是能找到类型的prototype
- 原理就是 一层一层查找__proto__,如果constructor.prototype相等则返回true,一直没找到则返回false
- instance.[proto ...] === instance.constructor.prototype
{
/*
1.内部作用机制,是通过对象中的原型链中是不是能找到类型的prototype
2.原理就是 一层一层查找__proto__,如果constructor.prototype相等则返回true,一直没找到则返回false
3.instance.[__proto__ ...] === instance.constructor.prototype
*/
function instanceOf(left, right) {
//left,instanceof右表达式
var O = right.prototype;
//获取右表达式的原型的显示原型
left = left.__proto__;
while (true) {
//Object.prototype.__proto__ === null
if (left === null) {
return false;
}
if(O === left) {
return true
}
left = left.__proto__;
}
}
function D(){}
function C(){}
var o = new C()
console.log(instanceOf(o,D)) //false
console.log(instanceOf(o,C)) //true
console.log({} instanceof Object) //true
}