javascript get type/instance name

后端 未结 5 1902
一个人的身影
一个人的身影 2021-02-07 01:06

Is there a reliable way of getting the instance of a JavaScript object?

For example, relying on the fake \'obj.getInstance()\' function.

var         


        
5条回答
  •  有刺的猬
    2021-02-07 01:45

    Only tested on Chrome:

    function FooBar() {
    
    }
    
    var foobar = new FooBar();
    
    console.log(foobar.constructor.name);  // Prints 'FooBar'
    

提交回复
热议问题