Write a function that checks if a given value is an instance of a given class or superclass. For this problem, an object is considered an instance of a given class if that object has access to that class's methods.
There are no constraints on the data types that can be passed to the function. For example, the value or the class could be undefined.
Example 1:
Input: func = () => checkIfInstanceOf(new Date(), Date) Output: true Explanation: The object returned by the Date constructor is, by definition, an instance of Date.
Example 2:
Input: func = () => { class Animal {}; class Dog extends Animal {}; return checkIfInstanceOf(new Dog(), Animal); }
Output: true
Explanation:
class Animal {};
class Dog extends Animal {};
checkIfInstanceOf(new Dog(), Animal); // true
Dog is a subclass of Animal. Therefore, a Dog object is an instance of both Dog and Animal.
Example 3:
Input: func = () => checkIfInstanceOf(Date, Date) Output: false Explanation: A date constructor cannot logically be an instance of itself.
Example 4:
Input: func = () => checkIfInstanceOf(5, Number) Output: true Explanation: 5 is a Number. Note that the "instanceof" keyword would return false. However, it is still considered an instance of Number because it accesses the Number methods. For example "toFixed()".
In this approach, we make use of direct language features to determine if an object is an instance of a given class. Most programming languages have built-in operators or functions to check instanceof relationships, such as 'instanceof' in JavaScript, or 'isinstance' in Python.
In JavaScript, the 'instanceof' operator checks if an object is an instance of a class. It walks up the prototype chain to see if the constructor appears anywhere in the object's prototype chain. This method is suitable for object instances and covers cases such as subclass inheritance.
Python
Java
C#
C
Time Complexity: O(n), where n is the depth of prototype chain.
Space Complexity: O(1), no extra space is used.
This approach is specifically for JavaScript where we manually traverse the prototype chain of the object to check if the class prototype is found in the chain.
This solution manually walks up the prototype chain by using 'Object.getPrototypeOf'. For each step in the chain, it verifies if the current prototype matches the prototype of the class, which ensures that subclass checks are also performed. This method provides finer control and understanding of the checking process.
C++
C
Time Complexity: O(n), where n is the depth of the prototype chain.
Space Complexity: O(1)
| Approach | Complexity |
|---|---|
| Approach 1: Use Language-Specific Instance Checking | Time Complexity: O(n), where n is the depth of prototype chain. |
| Approach 2: Manual Prototype Chain Walking (JavaScript Specific) | Time Complexity: O(n), where n is the depth of the prototype chain. |
Stop solving 500+ Leetcode problems • Sahil & Sarra • 512,531 views views
Watch 9 more video solutions →Practice Check if Object Instance of Class with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor