How to get the Class Name of an Object in JavaScript

Total
13
Shares

The constructor method in JavaScript is a special method of a class for creating and initializing an object instance of that class. The constructor will have the same name as the class name so we can use this to get the Class Name of an object in Javascript using the constructor name.

How to get the Class Name of an Object in JavaScript?

Before we start exploring the answer to this question, let’s familiarize ourselves with some basic concepts.

Class: They are templates/prototypes for creating objects.

Object: It is an entity that has a particular state and behaviour

Constructor: It is a member function of the class. Invoked during the creation of an object using the new keyword. The purpose of a constructor is to initialize a new object and set values for any existing object properties.

Now that basics are clear let us take a look at different solutions available to find the Class Name of an Object in JavaScript with examples.

Solution 1: Get Class Name of an Object Using the constructor Function

While creating an object of the class, the corresponding constructor is called either explicitly or implicitly. If no constructor is defined, the system invokes the default constructor. This object created can be used to return the class name by calling the name property of the constructor function.

*Fact Check: constructor is a function whereas name is a property

Syntax

[objectName].constructor.name

Let’s understand this with an example.

class Language {}

const l1 = new Language();

console.log(l1.constructor.name);

Output

Language

In the above code snippet, we are creating an empty class named “Language“. The second line shows constructor invocation for object creation and storing the reference in variable l1. We can create the object of Language class by using the new keyword and call its default constructor Language().

Now that we have the object, let’s get the object’s class name. For this, we can use the “obj. constructor” function as explained above. It returns a reference to the constructor from which the object got created. It does not refer to the name of the class directly.

To get the specific class name of the object, we have to use the “name” property of the constructor function.

Solution 2: Get Class Name of an Object Using Function inside the class

Another way of retrieving the class name of an object can be by creating a function inside the class that will return the object name. We need to use this keyword that will reference to the current object and of which getClassName() method is being called and then it returns it’s class name.

Syntax

this.constructor.name

Let’s understand this with an example.

class Language {
 getClassName() {
  return this.constructor.name;
 }
}

const l1 = new Language ();

const objClassName = l1.getClassName();

console.log(objClassName);

Output

Language

 In the above example, we are retrieving the object class name through custom functions rather than using the constructors directly.The function “getClassName()“, as shown in the above example, also uses the Javascript built-in constructor function. Here we use .this keyword so that it returns the current object name in scope.

Firstly, we start by creating and referencing the object of the class Language. Using the reference object, we invoke the method getClassName().

*Fact check: The approach works for objects created without a constructor function. They have a constructor property that points to the main Object constructor for the specific type.

Let’s have a look at the code below.

console.log([].constructor.name); 

// Output: Array

Conclusion

We can get the Class Name of an Object in JavaScript by creating an instance of a class and using constructor method’s name property and another approach is by creating our own function and using this.constructor.name inside it.

Both the above-mentioned solutions deliver the same output. The difference lies in the problem statement that we are addressing. We can opt for the first solution when referring to the direct class-object type relationship. The second solution is suitable for referring to class names through a custom function.

1 comment
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified on the latest articles

You May Also Like