AlgoDaily

Implement Instance Of

Read at your own pace. Code execution, quizzes, progress tracking, and the AI tutor are available in the interactive view with JavaScript enabled.

Question

In Object-Oriented programming, an instance represents an object which is an instantiation of a class. An object can also be an instance of a given class A if it is instantiated from a class B that extends the class A.

In a non-programming context, you could think of "cat" as a class and your particular cat as an instance of that class.

Many object-oriented programming languages have a built-in way of checking if a given object is an instance of a given class, and in Javascript this can be achieved by checking if the prototype property of a constructor appears anywhere in the prototype chain of an object.

Can you implement a function that will check if an object is an instance of a class, and will accept the object and the class as parameters?

The function should work like the following example:

class A {}
class B extends A {}

let objB = new B()
instanceOfClass(objB , B) // true
instanceOfClass(objB, A) // true

class C {}
instanceOfClass(objB, C) // false

cover

Solution

Solution

Our solution is pretty simple, it is a method that will first check the validity of the object, and then check if it is actually a prototype of the class' constructor.

If the object does not exist or we've reached the base object constructor, we will return false, since clearly it is not an instance of the given class.

if(!obj || typeof obj !== 'object') return false

The second check we are going to perform is if the given parameter is a valid object, and if it is not, then we are going to throw an error.

if(!target.prototype) throw Error

Finally, to check if our object is an instance of the given class, we are going to check if the object's prototype matches the class' prototype, and if it does - return true. Otherwise, we will call the method recursively to recurse down the prototypal chain, and check if the object is from a class that is extending our target class.

if (Object.getPrototypeOf(obj) === target.prototype) {
    return true
} else {
    return instanceOfClass(Object.getPrototypeOf(obj), target)
}

This being said, our final solution would look like this:

function instanceOfClass(obj, targetClass) {
    if (!obj || typeof obj !== 'object') return false
    if (!target.prototype) throw Error

    if (Object.getPrototypeOf(obj) === target.prototype) {
        return true
    } else {
        return instanceOfClass(Object.getPrototypeOf(obj), target)
    }
}

One Pager Cheat Sheet

One Pager Cheat Sheet

  • You can implement a function that will check if an object is an instance of a class, taking an object and a class as parameters, and checking if the prototype property of the constructor appears anywhere in the prototype chain of the given object.
  • We are writing a function to check if an object is an instance of a given class, by first checking its validity and then checking if it is a prototype of the class' constructor, and if not, recursing down the prototypal chain to check it.
Open interactive view →