Last updated: Mar 2, 2024
Reading timeยท3 min
Access the name
property on the object's constructor to get the class name
of the object, e.g. obj.constructor.name
.
The constructor
property returns a reference to the constructor function
that created the instance object.
class Person {} const p1 = new Person(); console.log(p1.constructor.name); // ๐๏ธ Person console.log(Person.name); // ๐๏ธ Person
We accessed the name
property on the
Object.constructor
property.
The Object.constructor
property returns a reference to the constructor
function from which the object was created.
class Person {} const p1 = new Person(); // โ The function (class) that created the object console.log(p1.constructor); // ๐๏ธ [class Person] // โ The class name of the object (string) console.log(p1.constructor.name); // ๐๏ธ "Person"
An alternative approach is to create a method on the class to get the class name of the object.
class Person { getClassName() { return this.constructor.name; } } const p1 = new Person(); const className = p1.getClassName(); console.log(className); // ๐๏ธ Person
If you need to access the Object.constructor.name
property often, create a
method that hides it.
Object.create(null)
) have a constructor property.Objects that are created without a constructor function have a constructor
property that points to the main Object
constructor for the specific type.
console.log({}.constructor.name); // ๐๏ธ Object console.log([].constructor.name); // ๐๏ธ Array
name
property directly on the classYou can also access the name
property directly on the class to get its name as
a string.
class Person {} console.log(Person.name); // ๐๏ธ Person
You can check if an object was created using a specific class in multiple ways.
The obj.constructor.name === Person.name
option should be preferred when you
minify your code for production.
class Person {} const p1 = new Person(); if (p1.constructor.name === 'Person') { // ๐๏ธ this runs console.log("The object's constructor is Person"); } // -------------------------------------------------- // โ If you minify your code for production, do this if (p1.constructor.name === Person.name) { // ๐๏ธ this runs console.log("The object's constructor is Person"); }
The first if
statement accesses the constructor.name
property to get the
class name of the object and compares it to the string "Person".
class Person {} const p1 = new Person(); if (p1.constructor.name === 'Person') { // ๐๏ธ this runs console.log("The object's constructor is Person"); }
This would work most of the time, however, if possible, it is better to compare
the object's class name to Person.name
.
class Person {} const p1 = new Person(); // โ If you minify your code for production, do this if (p1.constructor.name === Person.name) { // ๐๏ธ this runs console.log("The object's constructor is Person"); }
If you minify your code for production, the name of the class might change. This
isn't an issue when you access the name
property on the class in the
comparison.
You can also define a static
method that you can access directly on the class
to get the class name of an object.
class Person { static getClassName() { return 'Person'; } getClassName() { return Person.getClassName(); } } const p1 = new Person(); const className = p1.getClassName(); console.log(className); // ๐๏ธ Person console.log(Person.getClassName()); // ๐๏ธ Person
The static
keyword defines a static method or field. Static methods cannot be
directly accessed on instances of the class and must be accessed on the class
itself.
Our static method returns the string Person
and our instance method calls the
static method to return the same value.
If you need to check if an object is an instance of a class at runtime, use the
instanceof
operator.
class Person {} const p1 = new Person(); class Animal {} console.log(p1 instanceof Person); // ๐๏ธ true console.log(p1 instanceof Animal); // ๐๏ธ false
The instanceof
returns true
if the prototype
property of a constructor appears in the
prototype chain of an object and false
otherwise.
You can learn more about the related topics by checking out the following tutorials: