Skip to main content

Class Checking "instanceof"

Classes: Class Checking

What does the instanceof operator do in JavaScript?

View Answer:
Interview Response: The instanceof operator allows us to check whether an object belongs to a certain class. It also takes inheritance into account. Such a check may be necessary in many cases, and it can be used for building a polymorphic function, the one that treats arguments differently depending on their type.

Code Example:

class Rabbit {}
let rabbit = new Rabbit();

// is it an object of Rabbit class?
alert(rabbit instanceof Rabbit); // true

// It also works with constructor functions:

// instead of class
function Rabbit() {}

alert(new Rabbit() instanceof Rabbit); // true

// And with built-in classes like Array:

let arr = [1, 2, 3];
alert(arr instanceof Array); // true
alert(arr instanceof Object); // true

Is an Array an instance of an Object, in JavaScript?

View Answer:
Interview Response: Yes, an Array belongs to the Object class, because Array prototypically inherits from Object. At the base of JavaScript, all native objects like Array and even Function inherit from the Object class. The instanceof operator examines the prototype chain to render a result as true or false.

Code Example:

console.log(Array instanceof Object); // true
console.log(Function instanceof Object); // true

Is it possible to make a custom obj instanceof Class algorithm?

View Answer:
Interview Response: Yes, you can create a rough representation of the algorithm using a static method of Symbol.hasInstance, then just call it. It should return true or false as a result, by default it should be set to true. Most classes do not have Symbol.hasInstance. In that case, the standard logic is used: obj instanceOf Class checks whether Class.prototype is equal to one of the prototypes in the obj prototype chain.

Code Example:

class Animal {
static [Symbol.hasInstance](obj) {
if (obj.canEat) return true;

let obj = { canEat: true };

alert(obj instanceof Animal); // true: Animal[Symbol.hasInstance](obj) is called

In plain words, can you explain what happens with an instanceof class check?

View Answer:
Interview Response: The obj instanceOf Class checks whether Class.prototype is equal to one of the prototypes in the obj prototype chain. If any answer is true, return true. If it does not reach true as a result and reaches the end of the chain, return false. The Class constructor itself does not participate in the check! Only the chain of prototypes and Class.prototype matters.

Code Example:

class Rabbit extends Animal {}

let rabbit = new Rabbit();
alert(rabbit instanceof Animal); // true

// rabbit.__proto__ === Rabbit.prototype
// rabbit.__proto__.__proto__ === Animal.prototype (match! return true)

We already know that plain objects are converted to string as [object Object]. Is there a way to create a custom toString result for our custom objects?

View Answer:
Interview Response: Yes, the behavior of Object toString can be customized using a special object property Symbol.toStringTag. The Symbol.toStringTag also works for environment-specific objects like the window and XMLHttpRequest objects.

Code Example:

let user = {
[Symbol.toStringTag]: 'User',

alert({}; // [object User]

// toStringTag for the environment-specific object and class:
alert(window[Symbol.toStringTag]); // Window
alert(XMLHttpRequest.prototype[Symbol.toStringTag]); // XMLHttpRequest

alert({}; // [object Window]
alert({} XMLHttpRequest())); // [object XMLHttpRequest]