Sometimes you need to retrieve the name of a class in JavaScript. This is helpful for debug messages or when using the class name as an identifier. Retrieving the class name in JavaScript is different for static and instance methods.
This tutorial walks you through the individual method types and shows you how to access the class name.
Node.js Series Overview
- Increase the Memory Limit for Your Process
- Why You Should Add “node” in Your Travis Config
- Create a PDF from HTML with Puppeteer and Handlebars
- Create Your Own Custom Error
- Extend Multiple Classes (Multi Inheritance)
- Get a File’s Created Date
- Get a File’s Last Modified/Updated Date
- Write a JSON Object to a File
- How to Create an Empty File
- Generate a Random Number in Range With JavaScript/Node.js
- How to Merge Objects
- Retrieve a Request’s IP Address in Node.js
- Detect the Node.js Version in a Running Process or App
- Install Dependencies for a Specific Package
- String Replace All Appearances
- Remove All Whitespace From a String in JavaScript
- Generate a Random String in Node.js or JavaScript
- Remove Extra Spaces From a String in JavaScript or Node.js
- Remove Numbers From a String in JavaScript or Node.js
- Get the Part Before a Character in a String in JavaScript or Node.js
- Get the Part After a Character in a String in JavaScript or Node.js
- How to Check if a Value is a String in JavaScript or Node.js
- Check If a String Includes All Strings in JavaScript/Node.js/TypeScript
- How to Run an Asynchronous Function in Array.map()
- How to Reset and Empty an Array
- for…of vs. for…in Loops
- Clone/Copy an Array in JavaScript and Node.js
- Get an Array With Unique Values (Delete Duplicates)
- Sort an Array of Integers in JavaScript and Node.js
- Sort a Boolean Array in JavaScript, TypeScript, or Node.js
- Check If an Array Contains a Given Value in JavaScript or Node.js
- Add an Item to the Beginning of an Array in JavaScript or Node.js (Coming soon)
- Append an Item at the End of an Array in JavaScript or Node.js (Coming soon)
- Callback and Promise Support in your Node.js Modules
- Run Async Functions/Promises in Sequence
- Run Async Functions/Promises in Parallel
- Run Async Functions in Batches
- How to Fix “Promise resolver undefined is not a function” in Node.js or JavaScript
- Detect if Value Is a Promise in Node.js and JavaScript
- Retrieve the Class Name at Runtime in JavaScript and Node.js
- How to Check if an Object is Empty in JavaScript or Node.js
- How to CamelCase Keys of an Object in JavaScript or Node.js (Coming soon)
In Static Methods
Retrieving the class name in static methods is pretty straightforward. Because JavaScript represents classes as functions, you can access the name
property to retrieve the name of the class.
Retrieve the class name in static methods by either using this.name
or Class.name
:
class User {
/**
* Returns the string 'User'.
*
* @returns {String}
*/
static className() {
return this.name
// is the same as
// return User.name
}
}
console.log(
User.className()
)
// 'User'
Be Aware of a Static “name“ Property in Classes
There’s a pitfall you should be aware of. When defining a static name
property in your class, it will overwrite the name property of the class.
class User {
/**
* Returns the name.
*
* @returns {String}
*/
static get name () {
return 'Marcus'
}
/**
* Returns the string 'Marcus' because the static `name`
* property overwrites the name property of the class.
*
* @returns {String}
*/
static className() {
return this.name
}
}
console.log(
User.className()
)
// 'Marcus'
It’s recommended to use a static name
property with caution when working with JavaScript’s classes. It can be a helpful way to customize the class name to your own needs.
In Instance Methods
You can access the class name in instance methods using either the constructor or the class itself. The constructor is available via this.constructor
and provides the class properties:
class User {
/**
* Returns the string 'User'.
*
* @returns {String}
*/
className() {
return this.constructor.name
// is the same as
// return User.name
}
}
console.log(
User.className()
)
// 'User'
Text
Having a Static “name“ Property in Classes
Please notice, having a static name
property on your class overrides the actual class name:
class User {
/**
* Returns the name.
*
* @returns {String}
*/
static get name () {
return 'Marcus'
}
/**
* Returns the string 'Marcus' because the static `name`
* property overwrites the name property of the class.
*
* @returns {String}
*/
static className() {
return this.constructor.name
}
}
console.log(
User.className()
)
// 'Marcus'
That’s it about accessing the class name from static and instance methods. If you know of another way to access the class name, please share it in the comments below!