A instanceof B, A是否在B的原型链上
const new_instanceof = function (instance, constructor) {
let instance_proto = instance.__proto__;
let constructor_proto = constructor.prototype;
while(true) {
// 找到终点返回false
if (instance_proto === null) {return false};
// 找到返回true
if (instance_proto === constructor_proto) {return true};
// 当实例与构造函数原型不相同, 沿着原型链继续向上查找
instance_proto = instance_proto.__proto__;
}
}
console.log([] instanceof Array) // true
console.log(new_instanceof([], Array)) // true