Which operator checks class membership at runtime?

Get ready for your Object‑Oriented Programming Test. Use flashcards and multiple-choice questions. Each question includes hints and explanations. Prepare for your exam today!

Multiple Choice

Which operator checks class membership at runtime?

Explanation:
This tests runtime class membership checks using the instanceof operator. It tests whether a given object was created by a particular class (or constructor) by looking at the object’s prototype chain. If the prototype of the class appears somewhere in that chain, the check returns true. For example, if you have an object created with new MyClass(), then obj instanceof MyClass yields true. This works not only for exact classes but also for subclasses, since a subclass's prototype is linked into the chain. It’s different from typeof, which returns a string describing a primitive category (like "object" or "number") and isn’t about whether an object is an instance of a class. The other option isn’t a real language operator for this purpose in common OO languages.

This tests runtime class membership checks using the instanceof operator. It tests whether a given object was created by a particular class (or constructor) by looking at the object’s prototype chain. If the prototype of the class appears somewhere in that chain, the check returns true.

For example, if you have an object created with new MyClass(), then obj instanceof MyClass yields true. This works not only for exact classes but also for subclasses, since a subclass's prototype is linked into the chain.

It’s different from typeof, which returns a string describing a primitive category (like "object" or "number") and isn’t about whether an object is an instance of a class. The other option isn’t a real language operator for this purpose in common OO languages.