8

How to Get the Class Name of an Object in JavaScript

 1 year ago
source link: https://www.laravelcode.com/post/how-to-get-the-class-name-of-an-object-in-javascript
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to Get the Class Name of an Object in JavaScript

  1251 views

  2 years ago

Javascript

Use the name Property

You can use the name property of the object's constructor function to get the name of the class that is used to instantiated the object. Let's take a look an example:

<script>    
class Rectangle {
    // Class constructor
    constructor(length, width) {
        this.length = length;
        this.width = width;
    }
    
    // Class method
    getArea() {
        return this.length * this.width;
    }
}
 
var rectObj = new Rectangle(5, 10);
alert(rectObj.getArea()); // Outputs: 50
alert(rectObj.constructor.name) // Outputs: Rectangle
</script>
Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK