How To Get The Class Name Of An Object In Javascript

How to get the Class Name of an Object in JavaScript

There isn’t a straightforward way to retrieve your class name in JavaScript. However, we can know how to get the class name of an object in JS by combining a few accessible methods.

Get the class name of an object in JavaScript

Method 1: Built-in constructor name

In JavaScript, every object has a constructor named after the class or object itself.

Object.prototype.constructor:

The constructor property gives back a pointer to the Object constructor method, which generates a new instance of the object.
It should be noted that the value of this attribute refers to the function itself, not just a string with the function’s name in it.

Function.prototype.name:

The name attribute, which displays the read-only name supplied for a function (as an object).
It is possible for this “name” to be ‘anonymous’ or ” (empty string).

We’ll run our code in the following way:

class Something {}

NewThing = new Something();
console.log(NewThing.constructor.name);

Output

Something

You can always make a function within your class as follows:

class Something {
    getName() {
        return this.constructor.name;
    }
}
NewThing = new Something();
console.log(NewThing.getName());

Additionally, you can get the name of the class directly as follows:

class Something{}
console.log(Something.name);

They will give the same result.

Output

Something

Method 2: Without using function name

Alternately, the function.prototype.name method might not work with all browser versions. This strategy could work with browsers from before ES2015. Even when we use object.prototype.constructor, we can still tell what class we are using.

class Something {}
NewThing = new Something();
console.log(NewThing.constructor.toString());

Output

[class Something]

The written text is not an object that can be filtered. Instead of a string, prototype.constructor delivers a function replica. Since toString() stringifies every element of the object you call, using it alone might result in a prolonged string.

class Something {}
NewThing = new Something();
console.log(NewThing.constructor.toString().match(/ (\w+)/)[1]);

Output

Something

Code explanation:

First, the object.prototype.constructor delivers a reference to the constructor of the Something class, which created Newthing.

Next, a string that represents the function is returned.

Class Something{ }

We only pick the first occurrence that fits the pattern when we filter out the class name using the match() function, which starts from the blank space, hence [1].

Summary

The later versions of browsers that support the latest ES patches will be more likely to have a direct approach to how to get the class name of an object in JavaScript. Otherwise, we have covered the uses and understanding of prototype functions and properties of objects in JavaScript to mix and match them well.

Maybe you are interested:

Leave a Reply

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