Home >Web Front-end >JS Tutorial >How to Get the Class Name of an Object in JavaScript?

How to Get the Class Name of an Object in JavaScript?

Barbara Streisand
Barbara StreisandOriginal
2024-12-08 13:27:17422browse

How to Get the Class Name of an Object in JavaScript?

In JavaScript, there is no direct equivalent to Java's 'class.getName()' method for obtaining the name of an object's class.

ES2015 Update:

class Foo {}
console.log(Foo.name); // "Foo"

For non-class objects, the 'class' designation can be found using the 'thing.constructor.name' property.

var obj = {name: "John"};
console.log(obj.constructor.name); // "Object"

Hacks and Alternatives:

While there is no native 'class.getName()' equivalent, there are various hacks and alternative methods for obtaining the class name:

Object.prototype.getName() Hack (not recommended):

This hack modifies the Object prototype and adds a 'getName()' function to all objects, enabling them to return their constructor name as a string.

Object.prototype.getName = function() {
  var funcNameRegex = /function (.{1,})\(/;
  var results = funcNameRegex.exec(this.constructor.toString());
  return results && results.length > 1 ? results[1] : "";
};

var obj = {name: "John"};
console.log(obj.getName()); // "Object"

Using the Constructor Property (problematic):

Every object has a 'constructor' property that points to the constructor function that created it.

var myArray = [1, 2, 3];
console.log(myArray.constructor === Array); // true

However, relying on the 'constructor' property is problematic because:

  • It may not provide accurate information in cases where inheritance is involved.
  • It breaks cross-frame and cross-window.

Using instanceof (also problematic):

The 'instanceof' operator can also be used for type checking.

var myArray = [1, 2, 3];
console.log(myArray instanceof Array); // true

However, 'instanceof' does not work for literal values and also breaks cross-frame and cross-window.

Using Object.prototype.toString:

Object.prototype.toString returns a string representation of the object's type.

console.log(Object.prototype.toString.call('abc')); // "[object String]"
console.log(Object.prototype.toString.call(/abc/)); // "[object RegExp]"

While this approach provides type information for built-in types, it returns 'Object' for all user-defined types.

Caveats:

All of the aforementioned methods have caveats and may not be reliable in all circumstances. Consider the object's construction method, especially when dealing with inheritance and anonymous functions.

The above is the detailed content of How to Get the Class Name of an Object in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn