Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get an object's class name at runtime

Tags:

typescript

Is it possible to get an object's class/type name at runtime using TypeScript?

class MyClass{}  var instance = new MyClass(); console.log(instance.????); // Should output "MyClass" 
like image 348
Adam Mills Avatar asked Nov 28 '12 20:11

Adam Mills


People also ask

How do I get my class name at run time?

You have to use this snippet code for object: yourObject. getClass(). getSimpleName();

How do I find the class name of an object?

If you have a JavaSW object, you can obtain it's class object by calling getClass() on the object. To determine a String representation of the name of the class, you can call getName() on the class.

How do I get an object at runtime?

Java provides three different ways to find the type of an object at runtime like instanceof keyword, getClass(), and isInstance() method of java. lang. Class.

How do I get the class name of an object in Python?

To get the class name of an instance in Python: Use the type() function and __name__ to get the type or class of the Object/Instance. Using the combination of the __class__ and __name__ to get the type or class of the Object/Instance.


Video Answer


1 Answers

Simple answer :

class MyClass {}  const instance = new MyClass();  console.log(instance.constructor.name); // MyClass console.log(MyClass.name);              // MyClass 

However: beware that the name will likely be different when using minified code.

like image 54
Mikael Couzic Avatar answered Sep 17 '22 11:09

Mikael Couzic