/ / ideas:
// 1. If the instance type passed is not a reference type, return false
// 2. Get the prototype of the instance and check if it === constructor prototype
// 3. If not, exit loop until null is found and return false
function isInstanceof(instance, Constructor) {
if (typeofinstance ! = ='function'
&& (typeofinstance ! = ='object' || typeof instance === null)) {return false }
let proto = Object.getPrototypeOf(instance);
while (true) {
if (proto === null) {
return false
}
if (proto === Constructor.prototype) {
return true
}
proto = Object.getPrototypeOf(proto); }}function Animal(name) { this.name = name }
function Cat(name) { Animal.call(this, name) }
Cat.prototype = Object.create(Animal.prototype);
Cat.prototype.constructor = Cat;
const cat = new Cat('kimi');
console.log(isInstanceof(cat, Cat)); // true
console.log(isInstanceof(cat, Animal)); // true
console.log(isInstanceof(cat, Object)); // true
Copy the code