How to get a JavaScript object's class?

前端 未结 18 2184
一生所求
一生所求 2020-11-22 15:44

I created a JavaScript object, but how I can determine the class of that object?

I want something similar to Java\'s .getClass() method.

相关标签:
18条回答
  • 2020-11-22 16:27
    obj.constructor.name
    

    is a reliable method in modern browsers. Function.name was officially added to the standard in ES6, making this a standards-compliant means of getting the "class" of a JavaScript object as a string. If the object is instantiated with var obj = new MyClass(), it will return "MyClass".

    It will return "Number" for numbers, "Array" for arrays and "Function" for functions, etc. It generally behaves as expected. The only cases where it fails are if an object is created without a prototype, via Object.create( null ), or the object was instantiated from an anonymously-defined (unnamed) function.

    Also note that if you are minifying your code, it's not safe to compare against hard-coded type strings. For example instead of checking if obj.constructor.name == "MyType", instead check obj.constructor.name == MyType.name. Or just compare the constructors themselves, however this won't work across DOM boundaries as there are different instances of the constructor function on each DOM, thus doing an object comparison on their constructors won't work.

    0 讨论(0)
  • 2020-11-22 16:27

    I suggest using Object.prototype.constructor.name:

    Object.defineProperty(Object.prototype, "getClass", {
        value: function() {
          return this.constructor.name;
        }
    });
    
    var x = new DOMParser();
    console.log(x.getClass()); // `DOMParser'
    
    var y = new Error("");
    console.log(y.getClass()); // `Error'
    
    0 讨论(0)
  • 2020-11-22 16:28

    If you need to not only GET class but also EXTEND it from having just an instance, write:

    let's have

     class A{ 
       constructor(name){ 
         this.name = name
       }
     };
    
     const a1 = new A('hello a1');
    

    so to extend A having the instance only use:

    const a2 = new (Object.getPrototypeOf(a)).constructor('hello a2')
    // the analog of const a2 = new A()
    
    console.log(a2.name)//'hello a2'
    
    0 讨论(0)
  • 2020-11-22 16:28

    Javascript is a class-less languages: there are no classes that defines the behaviour of a class statically as in Java. JavaScript uses prototypes instead of classes for defining object properties, including methods, and inheritance. It is possible to simulate many class-based features with prototypes in JavaScript.

    0 讨论(0)
  • 2020-11-22 16:30

    You can get a reference to the constructor function which created the object by using the constructor property:

    function MyObject(){
    }
    
    var obj = new MyObject();
    obj.constructor; // MyObject
    

    If you need to confirm the type of an object at runtime you can use the instanceof operator:

    obj instanceof MyObject // true
    
    0 讨论(0)
  • 2020-11-22 16:31

    You can also do something like this

     class Hello {
         constructor(){
         }
        }
        
          function isClass (func) {
            return typeof func === 'function' && /^class\s/.test(Function.prototype.toString.call(func))
        }
        
       console.log(isClass(Hello))

    This will tell you if the input is class or not

    0 讨论(0)
提交回复
热议问题