Solutions To Get An Object’s Class In JavaScript

How to get an Object’s Class in JavaScript

In Javascript, not many functions support interaction between objects and classes. Here I will use some methods like the instanceof keywords and prototype.isPrototypeOf() to get an object’s class in JavaScript by checking if it is an object of that class. Follow the article to better understand.

Get An Object’s Class In JavaScript

Method 1: Use instanceof keywords

Instanceof keywords are used to check if this object belongs to this class, and the return result of instanceof keywords is a boolean value. If the returned result is true, we have the suitable object for the class we need.

Syntax:

myObj isntanceof className

Parameters:

  • myObj: Is the object to be checked
  • className: is the name of the class you want to check if the object myObj belongs to it

Example 1:

class Cls1 {}
class Cls2 {}

var myObj = new Cls1();

console.log(myObj instanceof Cls1);    
console.log(myObj instanceof Cls2); 

Output:

true
false

In the above example, we see that myObj is not of ‘Cls2’ class , so the return value of instanceof returns false. The result returns true when we test it with ‘Cls1’ class, from which we get myObj of ‘Cls1’ class.

Example 2:

class Animal {}
class Person {}

var dog = new Animal();

console.log(dog instanceof Animal);    
console.log(dog instanceof Person);

Output:

true
false

Similar to the example above, we see that ‘dog’ is not of the ‘Person’ class, so the return value of instanceof is false. In case we test it with the ‘Animal’ class, the result is true, so ‘dog’ is an object of the ‘Animal’ class. As in the two examples above, we can check whether an object belongs to that class or not, from which we get an object from a class you want.

Method 2: Use prototype.isPrototypeOf() method

This method is similar to method one and returns a boolean.

Syntax:

classname.prototype.isPrototypeOf(myObj)

Parameters:

  • myObj: Is the object to be checked
  • className: is the name of the class you want to check if the object myObj belongs to it

Example 1:

class Cls1 {}
class Cls2 {}

var myObj = new Cls1();

console.log(Cls1.prototype.isPrototypeOf(myObj));    
console.log(Cls2.prototype.isPrototypeOf(myObj));

Output:

true
false

When we check myObj with the prototype.isPrototypeOf(), we see that myObj is an object of Cls1, so the return result is true and vice versa because when compared with Cls2, we get the result false.

Example 2:

class Animal {}
class Person {}

var cat = new Animal();

console.log(Animal.prototype.isPrototypeOf(cat));    
console.log(Person.prototype.isPrototypeOf(cat)); 

Output:

true
false

Similarly, the cat is an animal, not a person, so the result of the first line is true, and is false otherwise. The cat is not a person, so the second line is false.

Summary

In this tutorial, I showed several ways to get an object’s class in JavaScript. I hope this article helps you and your program. If you have any questions or problems, please comment below. I will respond as possible. Thank you for your reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *