获取对象或类的名称 [英] Get name of object or class

查看:108
本文介绍了获取对象或类的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何解决方案来获取对象的函数名称?

Is there any solution to get the function name of an object?

function alertClassOrObject (o) {
   window.alert(o.objectName); //"myObj" OR "myClass" as a String
}

function myClass () {
   this.foo = function () {
       alertClassOrObject(this);
   }
}

var myObj = new myClass();
myObj.foo();

for(var k in this){...} - 没有关于 className ObjectName 的信息。有可能得到其中一个吗?

for (var k in this) {...} - there is no information about the className or ObjectName. Is it possible to get one of them?

推荐答案

获取对象构造函数函数,然后检查它的名称属性。

Get your object's constructor function and then inspect its name property.

myObj.constructor.name

返回myClass。

这篇关于获取对象或类的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆