function isInstanceof(instance, Constructor) {
if (typeof instance !== 'function'
&& (typeof instance !== '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));
console.log(isInstanceof(cat, Animal));
console.log(isInstanceof(cat, Object));