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:func= () => checkIfInstanceOf(5, Number)
Output:trueExplanation:5isa Number. Notethatthe"instanceof"keywordwouldreturnfalse. However, itisstillconsideredaninstanceof Number becauseitaccessesthe Number methods. Forexample"toFixed()".
We need to check if a value is an instance of a class, including primitives like numbers and strings, which can access class methods via boxing. We must also handle edge cases like undefined and null.
For objects, we can use the instanceof operator. For primitives, we check if their constructor matches the class or if they can access the class’s prototype methods. Special care is needed for null and undefined, which are not instances of any class.