Apr 13, 2023
Edit me

#. Problem

This problem is not an algorithm related, it’s more towards to understanding of the javascript.
The purpose of this is to remember this problem and solution.

Go to the problem on leetcode

1. My approach

Something obvious.

function checkIfInstanceOf(obj: any, classFunction: any): boolean {
  // or obj instanceof classFunction
    return typeof obj === typeof classFunction;
};

2. Solution

Every JS object has prototype, and every prototype has constructor.
So, we can track its parent to check if the object is an instance of the class.

function checkIfInstanceOf(obj: any, classFunction: any): boolean {
    if (obj === null || obj === undefined || !classFunction) {
        return false;
    }

    let ctor: any = obj.constructor;

    while (ctor) {
        if (ctor === classFunction) {
            return true;
        }
        ctor = Object.getPrototypeOf(ctor.prototype)?.constructor;
    }

    return false;
};

3. Epilogue

Honestly, in production, this kind of problem is not something common.
However, it is still valid to have a good understanding of programming languages.

What I’ve learned from this exercise:

  • importancy of understanding the language