Get the name of an object's type

前端 未结 20 2258
忘掉有多难
忘掉有多难 2020-11-21 22:37

Is there a JavaScript equivalent of Java\'s class.getName()?

20条回答
  •  不知归路
    2020-11-21 22:52

    Jason Bunting's answer gave me enough of a clue to find what I needed:

    <>.constructor.name
    
    
    

    So, for example, in the following piece of code:

    function MyObject() {}
    var myInstance = new MyObject();
    

    myInstance.constructor.name would return "MyObject".

    提交回复
    热议问题