Here is the interview question prompt, presented for reference.
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
You can see the full challenge with visuals at this link.
Challenges • Asked over 3 years ago by Jake from AlgoDaily
This is the main discussion thread generated for Implement Instance Of (Main Thread).