0

How do i get the own classname of a class in javascript?

I implemented a method and i want to catch an error by logging the error with a message in which class the error happened.

My current solution is dirty: I save the class-name in the constructor

constructor{
    this.className = 'MyClass';}

However, i think there must be a better solution, something like Object.protytpe.XXX

I googled and searched in Stackoverflow, but all the answers didnt result in my desired output.

May somebody have an idea?

Jimbo.Koen
  • 141
  • 8

1 Answers1

1

Use:

  • this.constructor.name from inside an instance method of your class
  • obj.constructor.name where obj is an instance of your class
  • klass.name where klass is a reference to your class