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 #2618 Check if Object Instance of Class, the goal is to determine whether a given object is an instance of a specified class. This problem focuses on understanding how JavaScript’s prototype chain works internally.
The key idea is to traverse the object's prototype chain using Object.getPrototypeOf(). Every object in JavaScript has an internal prototype that links to its parent. By repeatedly moving up this chain, you can check if any prototype matches classFunction.prototype. If a match is found, the object is considered an instance of that class.
Edge cases are important: values like null or undefined do not have prototypes, so they should immediately return false. The traversal continues until the prototype becomes null, which indicates the top of the chain.
This approach mirrors how the instanceof operator works internally. Since the prototype chain depth is usually small, the algorithm runs efficiently with O(h) time complexity where h is the height of the prototype chain, and O(1) space.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Prototype Chain Traversal using Object.getPrototypeOf | O(h) | O(1) |
Sahil & Sarra
Use these hints if you're stuck. Try solving on your own first.
In Javascript, inheritance is achieved with the prototype chain.
You can get the prototype of an object with the Object.getPrototypeOf(obj) function. Alternatively, you can code obj['__proto__'].
You can compare an object's __proto__ with classFunction.prototype.
Traverse the entire prototype chain until you find a match.
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.
Time Complexity: O(n), where n is the depth of prototype chain.
Space Complexity: O(1), no extra space is used.
1public static bool CheckIfInstanceOf(object obj, Type type) {
2 return type.IsInstanceOfType(obj);
3}C# provides the 'IsInstanceOfType' method on the 'Type' class to check if an object is of a specified type or its subclass.
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.
Time Complexity: O(n), where n is the depth of the prototype chain.
Space Complexity: O(1)
1// Similar to the first approach, C does not support object-oriented paradigms and prototype chains are not applicable.Without native support for object-oriented programming paradigms and prototype chain traversal as found in JavaScript, C requires custom patterns and structures to simulate object-like behavior, if needed.
Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
JavaScript objects inherit properties through the prototype chain. Checking whether a class's prototype appears in that chain effectively determines if the object is an instance of the class.
Questions about prototype chains, object inheritance, and instanceof behavior are common in JavaScript-focused interviews at large tech companies. This problem tests your understanding of how JavaScript's object model works internally.
The optimal approach is to traverse the object's prototype chain and compare each level with the target class's prototype. Using Object.getPrototypeOf allows you to move up the chain until you find a match or reach null.
You should handle cases where the object is null or undefined since they do not have prototypes. These values should immediately return false to avoid runtime errors during prototype traversal.