
This is useful because it allows you to get the name of the class that created an object, which can be useful for debugging. In this post, we'll learn how you can get the class name of an object in JavaScript by using the constructor property. HTMLCollection An HTMLCollection is an array-like collection (list) of HTML elements. The getElementsB圜lassName () property is read-only. The getElementsB圜lassName () method returns an HTMLCollection. The reason this works is you can use the constructor property to get the class of an object, which includes the name, along with other properties.īecause most things in JavaScript are objects, they will therefore have a valid constructor property. The getElementsB圜lassName () method returns a collection of elements with a specified class name (s). Access the name property on the objects constructor to get the class name of the object, e.g. You can loop through these classes and get/check for the one you required. attr('class') will give you all the classes associated to that element. Getting the class name of an objectįirst let's start with a class and an object created from it. KunalTyagi Sorry for late but you cannot do that since className or. In this post, we'll learn how you can get the class name of an object in JavaScript.

When the JavaScript get element by class name method is. One useful thing to be able to know from an object is the name of the class it was created from. The JavaScript getElementsB圜lassName is used to get all the elements that belong to a particular class. Return obj.toString().match(/ (\w+)/) Ĭonsole.log("constructor:", what(p.constructor), "name:", p.constructor.name, "class:", what(p)) įunction Player() else if (type = 'function' & ns.JavaScript supports object-oriented programming, which means that you can create classes and objects, and use them to store data.

Matching (\w+) should satisfy ES6 style classes. Method 1: Get the Class Name Using Name Property First, a class called Teacher is created through an empty body. Or maybe you should ask yourself "Why am I doing this? Is this really what I am trying to solve?"

If the following adds to the complexity of your decision then maybe you're looking for instanceof. Key in your roll number and password Post logging in, score card will be available on screen Go through marksheet and download the same Take its printout for future reference PSEB Class 5th Result 2023: Official websites to check pseb.ac. On homepage, click on Class 5 result link. There is a difference between the constructor and the actual object/class name. Visit to the official PSEB website pseb.ac.in. As this was already answered, I just wanted to point out the differences in approaches on getting the constructor of an object in JavaScript.
