
In this task, your objective is to implement a function checkIfInstanceOf that determines whether a given value is an instance of a specified class or any of its superclasses. This concept is rooted in object-oriented programming, where objects inherit behavior from their constructors and their prototype chains.
However, unlike JavaScript's built-in instanceof operator, your implementation must be more flexible:
false for invalid inputs, such as when the constructor is undefined or not a function.This generalized approach ensures that your function can determine instance relationships reliably, even for unconventional cases that would typically fail with instanceof.
Input:
Output:
Explanation:
The object returned by the Date constructor is an instance of Date.
Input:
Output:
Explanation:Dog is a subclass of Animal. So, an instance of Dog is also an instance of Animal.
Input:
Output:
Explanation:
A constructor function like Date is not an instance of itself.
Input:
Output:
Explanation:
Although 5 is a primitive, it can access methods from the Number prototype, like toFixed(). Thus, it's considered an instance of Number in this context.
obj and constructor can be of any type — including undefined, null, or primitives.boolean.instanceof operator.To implement this logic, we need to replicate how JavaScript's prototype chain works, but with more inclusive input handling.
Before any logic runs, check if:
constructor is a function — if not, return false.obj is non-null and of type "object" or "function" — otherwise, box it using Object(obj) to access its prototype.This step ensures safety and allows handling of primitive values like numbers or strings.
Use Object.getPrototypeOf() to walk up the prototype chain of the given object. At each level:
constructor.prototype.true.null), return false.This mimics the internal behavior of instanceof.
Primitives like 5, 'hello', or true are not objects, but in JavaScript they can still access methods from their respective wrapper types (Number, String, Boolean). For example:
To account for this, box primitive values using Object(obj) before beginning the prototype walk.
checkIfInstanceOf(Date, Date) should return false — constructors are functions, not instances.undefined or null, return false safely.This problem highlights deep understanding of JavaScript's type system and prototype chain. By abstracting away the strictness of instanceof and handling edge cases explicitly, your function will be more robust and aligned with real-world behavior.
In JavaScript, to check if an object is an instance of a particular class, you can use the custom isInstanceOf function provided in the solution. This function determines if a given object inherits from a specific constructor's prototype, which is a useful alternative to JavaScript's native instanceof operator, especially in cases that involve different contexts, such as different iframes or realms where prototypes may not match.
Here’s how the solution works:
Generate Prototype Sequence:
generatePrototypes is defined to yield each prototype in the prototype chain of the given instance, starting from the object itself up to the top of the chain (null).Check Instance Against Constructor:
isInstanceOf, checks if the specified object is an instance of given constructor. This is done by iterating over the prototypes generated by generatePrototypes.null or undefined, or if the supposed constructor is not a function, returning false in those situations.Usage:
isInstanceOf by passing the object and the constructor function as arguments.true, the object is an instance of the class represented by the constructor function.false, the object is not an instance of that class.This approach employs JavaScript ES6 features, particularly generator functions, to provide a robust check for object-instance relationships, which can be particularly useful in complex applications where inheritance checks need to cross execution contexts.
0 Comments
Be the first to comment and share your perspective with the community.