How to get a JavaScript object's class?

前端 未结 18 2374
一生所求
一生所求 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: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

提交回复
热议问题