Warning: session_start(): open(/tmp/sess_503894b9893c035f1c00cd538b6d41b2, O_RDWR) failed: Disk quota exceeded (122) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: session_start(): Failed to read session data: files (path: /tmp) in /home/wvyrfnwn/learnshareit.com/wp-content/plugins/learnpress/inc/class-lp-page-controller.php on line 1007

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 719

Warning: ftp_mkdir() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 562

Warning: ftp_nlist() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 420

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230

Warning: ftp_pwd() expects parameter 1 to be resource, null given in /home/wvyrfnwn/learnshareit.com/wp-admin/includes/class-wp-filesystem-ftpext.php on line 230
How To Get The Class Name Of An Object In TypeScript - LearnShareIT

How To Get The Class Name Of An Object In TypeScript

Get the Class Name of an Object in TypeScript

Accessing the ‘name’ property of the object’s constructor can help you Get the Class Name of an Object in TypeScript. So how to do it? Let’s go in the detail!

Get The Class Name Of An Object In TypeScript

Method 1: Access the ‘name’ property of the object’s constructor

In TypeScript, we can get the name of an object by accessing the ‘name‘ property of the object’s constructor. 

Syntax: 

object.constructor.name

In a little more detail, object.constructor returns a reference to the object’s constructor function, which creates the instance object. We then access the ‘name’ property to get the object’s class name.

Take a look at this example:

class Laptop{}
 
const myLaptop = new Laptop();
 
const ClassName = myLaptop.constructor.name;
console.log(ClassName );
console.log(Laptop.name);

Output:

"Laptop"
"Laptop"

I have created an instance called myLaptop for the class Laptop. After that, I get the name of the Laptop class and myLaptop object’s class name using these commands in the above example. As you can see, we get the same value in the console. So we got the correct class name of the myLaptop object.

Method 2: Declare a property that stores the value of the class name

If you want to access the object’s name property frequently, declare a property that stores the value of the class name or a method to get the class name. 

Example – 2:

class Laptop{
  Name = this.constructor.name;
// or you can hard code the class name: Name = 'Laptop'
 
  getName(){
    return this.constructor.name;
   //or return 'Laptop'
  }
}
 
const myLaptop = new Laptop();
console.log(myLaptop.Name);
console.log(myLaptop.getName());

Output:

"Laptop"
"Laptop"

The approach above will make your code more readable and avoid the repetition of calling object.constructor.name.

Summary

Thank you for being so interested in the content of the article. I hope you understood how we could Get The Class Name Of An Object In TypeScript.

Maybe you are interested:

Leave a Reply

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