
Sponsored
Sponsored
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.
1In Python, 'isinstance' is a built-in function used to check if an object is an instance or subclass of a class. It directly returns true or false based on the check.
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// C++ does not provide dynamic runtime type information similar to JavaScript's prototype chain traversal. Instead, RTTI (Run-Time Type Information) mechanisms in C++ such as 'dynamic_cast' or typeid can be used for known inheritance hierarchies, albeit with different constraints.C++ employs extensive compile-time type checking and enforces static polymorphism, making prototype chain traversal as in JavaScript unsuitable and unavailable. Instead, C++ relies on virtual functions and RTTI features like 'dynamic_cast' for checked downcasting only within known hierarchical contexts.