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:
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

Try to solve this here or in Interactive Mode.
How do I practice this challenge?
xxxxxxxxxx
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)
}
}
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.
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.
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.
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:
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 anobject
is an instance of agiven 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.
xxxxxxxxxx
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)
}
}
Great job getting through this. Let's move on.
If you had any problems with this tutorial, check out the main forum thread here.