Check if Object Instance of Class - Problem
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.
Input & Output
Example 1 — Basic Object Instance
$
Input:
obj = {}, classFunction = Object
›
Output:
true
💡 Note:
Empty object {} has Object.prototype in its prototype chain, so it's an instance of Object
Example 2 — Null Class Function
$
Input:
obj = {}, classFunction = null
›
Output:
false
💡 Note:
When classFunction is null, we can't check instanceof, so return false
Example 3 — Number Instance
$
Input:
obj = 5, classFunction = Number
›
Output:
true
💡 Note:
Number 5 is an instance of the Number constructor function
Constraints
- No constraints on data types
- obj or classFunction can be undefined/null
- Must handle all JavaScript types
Visualization
Tap to expand
Understanding the Visualization
1
Input
Object and class function to check
2
Prototype Chain
Walk up the object's prototype chain
3
Match Check
Compare with class function's prototype
Key Takeaway
🎯 Key Insight: The instanceof operator works by walking up the prototype chain until it finds the constructor's prototype or reaches null
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code