Check if Object Instance of Class
Medium
Solution
export function checkIfInstanceOf(obj: any, classFunction: any): boolean {
if (obj === null || obj === undefined || typeof classFunction !== 'function') {
return false;
}
if (typeof obj === 'object') {
return obj instanceof classFunction;
}
return Object(obj) instanceof classFunction;
}Generator
function* prototypeGenerator(obj: any) {
let currentPrototype = Object.getPrototypeOf(obj);
while (currentPrototype !== null) {
yield currentPrototype;
currentPrototype = Object.getPrototypeOf(currentPrototype);
}
}
export function checkIfInstanceOf(obj: any, classFunction: any): boolean {
if (obj === null || obj === undefined || typeof classFunction !== 'function') {
return false;
}
for (const prototype of prototypeGenerator(obj)) {
if (prototype === classFunction.prototype) {
return true;
}
}
return false;
}