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

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的信息。有可能弄到一个吗?


当前回答

在运行时获得类名的最有效方法

let className = this.constructor.name

其他回答

我们需要的是:

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

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 ();/ /应用程序

I was facing a similar difficulty and none of the solutions presented here were optimal for what I was working on. What I had was a series of functions to display content in a modal and I was trying to refactor it under a single object definition making the functions, methods of the class. The problem came in when I found one of the methods created some nav-buttons inside the modal themselves which used an onClick to one of the functions -- now an object of the class. I have considered (and am still considering) other methods to handle these nav buttons, but I was able to find the variable name for the class itself by sweeping the variables defined in the parent window. What I did was search for anything matching the 'instanceof' my class, and in case there might be more than one, I compared a specific property that was likely to be unique to each instance:

var myClass = function(varName)
{
    this.instanceName = ((varName != null) && (typeof(varName) == 'string') && (varName != '')) ? varName : null;

    /**
     * caching autosweep of window to try to find this instance's variable name
     **/
    this.getInstanceName = function() {
        if(this.instanceName == null)
        {
            for(z in window) {
                if((window[z] instanceof myClass) && (window[z].uniqueProperty === this.uniqueProperty)) {
                    this.instanceName = z;
                    break;
                }
            }
        }
        return this.instanceName;
    }
}

在运行时获得类名的最有效方法

let className = this.constructor.name

试试这个:

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

因为这个问题已经被回答了,我只是想指出在JavaScript中获取对象构造函数的方法的差异。 构造函数和实际的对象/类名之间存在差异。如果下面的情况增加了您决策的复杂性,那么可能您正在寻找instanceof。或者你应该问问自己“我为什么要这样做?”这真的是我要解决的问题吗?”

注:

obj.constructor.name在旧的浏览器上不可用。 匹配(\w+)应该满足ES6样式类。

代码:

var what = function(obj) {
  return obj.toString().match(/ (\w+)/)[1];
};

var p;

// Normal obj with constructor.
function Entity() {}
p = new Entity();
console.log("constructor:", what(p.constructor), "name:", p.constructor.name , "class:", what(p));

// Obj with prototype overriden.
function Player() { console.warn('Player constructor called.'); }
Player.prototype = new Entity();
p = new Player();
console.log("constructor:", what(p.constructor), "name:", p.constructor.name, "class:", what(p));

// Obj with constructor property overriden.
function OtherPlayer() { console.warn('OtherPlayer constructor called.'); }
OtherPlayer.constructor = new Player();
p = new OtherPlayer();
console.log("constructor:", what(p.constructor), "name:", p.constructor.name, "class:", what(p));

// Anonymous function obj.
p = new Function("");
console.log("constructor:", what(p.constructor), "name:", p.constructor.name, "class:", what(p));

// No constructor here.
p = {};
console.log("constructor:", what(p.constructor), "name:", p.constructor.name, "class:", what(p));

// ES6 class.
class NPC { 
  constructor() {
  }
}
p = new NPC();
console.log("constructor:", what(p.constructor), "name:", p.constructor.name , "class:", what(p));

// ES6 class extended
class Boss extends NPC {
  constructor() {
    super();
  }
}
p = new Boss();
console.log("constructor:", what(p.constructor), "name:", p.constructor.name , "class:", what(p));

结果:

代码:https://jsbin.com/wikiji/edit?js,控制台