Back to course sections
    Mark As Completed Discussion

    Good afternoon! Here's our prompt for today.

    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:

    JAVASCRIPT
    1class A {}
    2class B extends A {}
    3
    4let objB = new B()
    5instanceOfClass(objB , B) // true
    6instanceOfClass(objB, A) // true
    7
    8class C {}
    9instanceOfClass(objB, C) // false

    Question

    Try to solve this here or in Interactive Mode.

    How do I practice this challenge?

    JAVASCRIPT
    OUTPUT
    :001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

    Here's how we would solve this problem...

    How do I use this guide?

    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.

    JAVASCRIPT
    1if(!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.

    JAVASCRIPT
    1if(!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.

    JAVASCRIPT
    1if (Object.getPrototypeOf(obj) === target.prototype) {
    2    return true
    3} else {
    4    return instanceOfClass(Object.getPrototypeOf(obj), target)
    5}

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

    JAVASCRIPT
    1function instanceOfClass(obj, targetClass) {
    2    if (!obj || typeof obj !== 'object') return false
    3    if (!target.prototype) throw Error
    4
    5    if (Object.getPrototypeOf(obj) === target.prototype) {
    6        return true
    7    } else {
    8        return instanceOfClass(Object.getPrototypeOf(obj), target)
    9    }
    10}

    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.

    This is our final solution.

    To visualize the solution and step through the below code, click Visualize the Solution on the right-side menu or the VISUALIZE button in Interactive Mode.

    JAVASCRIPT
    OUTPUT
    :001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment

    Great job getting through this. Let's move on.

    If you had any problems with this tutorial, check out the main forum thread here.