是否有任何解决方案来获得一个对象的函数名?

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();

对于(var k in this){…} -没有关于className或ObjectName的信息。有可能弄到一个吗?


当前回答

如果你使用标准的IIFE(例如TypeScript)

var Zamboch;
(function (_Zamboch) {
    (function (Web) {
        (function (Common) {
            var App = (function () {
                function App() {
                }
                App.prototype.hello = function () {
                    console.log('Hello App');
                };
                return App;
            })();
            Common.App = App;
        })(Web.Common || (Web.Common = {}));
        var Common = Web.Common;
    })(_Zamboch.Web || (_Zamboch.Web = {}));
    var Web = _Zamboch.Web;
})(Zamboch || (Zamboch = {}));

您可以在原型之前注释

setupReflection(Zamboch, 'Zamboch', 'Zamboch');

然后使用_fullname和_classname字段。

var app=new Zamboch.Web.Common.App();
console.log(app._fullname);

注释函数:

function setupReflection(ns, fullname, name) {
    // I have only classes and namespaces starting with capital letter
    if (name[0] >= 'A' && name[0] <= 'Z') {
        var type = typeof ns;
        if (type == 'object') {
            ns._refmark = ns._refmark || 0;
            ns._fullname = fullname;
            var keys = Object.keys(ns);
            if (keys.length != ns._refmark) {
                // set marker to avoid recusion, just in case 
                ns._refmark = keys.length;
                for (var nested in ns) {
                    var nestedvalue = ns[nested];
                    setupReflection(nestedvalue, fullname + '.' + nested, nested);
                }
            }
        } else if (type == 'function' && ns.prototype) {
            ns._fullname = fullname;
            ns._classname = name;
            ns.prototype._fullname = fullname;
            ns.prototype._classname = name;
        }
    }
}

小提琴

其他回答

如果你使用标准的IIFE(例如TypeScript)

var Zamboch;
(function (_Zamboch) {
    (function (Web) {
        (function (Common) {
            var App = (function () {
                function App() {
                }
                App.prototype.hello = function () {
                    console.log('Hello App');
                };
                return App;
            })();
            Common.App = App;
        })(Web.Common || (Web.Common = {}));
        var Common = Web.Common;
    })(_Zamboch.Web || (_Zamboch.Web = {}));
    var Web = _Zamboch.Web;
})(Zamboch || (Zamboch = {}));

您可以在原型之前注释

setupReflection(Zamboch, 'Zamboch', 'Zamboch');

然后使用_fullname和_classname字段。

var app=new Zamboch.Web.Common.App();
console.log(app._fullname);

注释函数:

function setupReflection(ns, fullname, name) {
    // I have only classes and namespaces starting with capital letter
    if (name[0] >= 'A' && name[0] <= 'Z') {
        var type = typeof ns;
        if (type == 'object') {
            ns._refmark = ns._refmark || 0;
            ns._fullname = fullname;
            var keys = Object.keys(ns);
            if (keys.length != ns._refmark) {
                // set marker to avoid recusion, just in case 
                ns._refmark = keys.length;
                for (var nested in ns) {
                    var nestedvalue = ns[nested];
                    setupReflection(nestedvalue, fullname + '.' + nested, nested);
                }
            }
        } else if (type == 'function' && ns.prototype) {
            ns._fullname = fullname;
            ns._classname = name;
            ns.prototype._fullname = fullname;
            ns.prototype._classname = name;
        }
    }
}

小提琴

例子:

function Foo () { console.log('Foo function'); } var f = new Foo(); console.log('f', f.constructor.name); // -> "Foo" var Bar = function () { console.log('Anonymous function (as Bar)'); }; var b = new Bar(); console.log('b', b.constructor.name); // -> "Bar" var Abc = function Xyz() { console.log('Xyz function (as Abc)'); }; var a = new Abc(); console.log('a', a.constructor.name); // -> "Xyz" class Clazz { constructor() { console.log('Clazz class'); } } var c = new Clazz(); console.log('c', c.constructor.name); // -> "Clazz" var otherClass = class Cla2 { constructor() { console.log('Cla2 class (as otherClass)'); } } var c2 = new otherClass(); console.log('c2', c2.constructor.name); // -> "Cla2"

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

myObj.constructor.name

myClass”了。

试试这个:

var classname = ("" + obj.constructor).split("function ")[1].split("(")[0];

我们需要的是:

在函数中封装一个常量(函数的名称等于我们想要获取的对象的名称) 在对象中使用箭头函数

console.clear (); function App(){//我的常量的名字是App 返回{ 答:{ b: { C:()=>{//这里很重要,使用箭头函数 console.log (this.constructor.name) } } } } } const obj = new App();/ /使用 obj.a.b.c ();/ /应用程序 //使用react道具等 //例如,我们想把这个回调函数传递给某个组件 const myComponent = {}; myComponent。customProps = obj.a.b.c; myComponent.customProps ();/ /应用程序