
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.
1functionIn JavaScript, the 'instanceof' operator checks if an object is an instance of a class. It walks up the prototype chain to see if the constructor appears anywhere in the object's prototype chain. This method is suitable for object instances and covers cases such as subclass inheritance.
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.