简体   繁体   English

JavaScript +获取对象/函数名称

[英]JavaScript + Get object / function name

I am building an app with JavaScript. 我正在使用JavaScript构建应用程序。 This app integrates with a third-party library. 该应用程序与第三方库集成。 That library defines an object like this: 该库定义了这样的对象:

getLibrary().SomeObject = function() {
  function SomeObject(attrs) {
    this.id = attrs.id;
    this.description = attrs.description || '';

    this.result = {
      id: this.id,
      description: this.description,
    };
  }

  SomeObject.prototype.doSomething = function(actual) {
    return 'do something';
  };

  SomeObject.prototype.calculate = function() {
    return 42;
  };

  return SomeObject;
};

I have an instance of this object in a variable called myInstance . 我在名为myInstance的变量中有此对象的实例。 When I do a console.log(myInstance) , I see the following in the Chrome console window: 当执行console.log(myInstance) ,我在Chrome控制台窗口中看到以下内容:

v SomeObject
  id: '1'
  description: 'my description'
  > result: Object

I am trying to get the "SomeObject" part from above. 我正在尝试从上面获取“ SomeObject”部分。 I tried: 我试过了:

if (myInstance instanceof SomeObject) {
  ...
}

However, that didn't work. 但是,那没有用。 I'm basically trying to get the name of the "type" (or function?). 我基本上是在尝试获取“类型”(或函数?)的名称。 How do I get that from myInstance ? 我如何从myInstance获得它?

Thanks 谢谢

getLibrary().SomeObject is not a constructor function, it's a function that returns a constructor function. getLibrary().SomeObject不是构造函数,它是一个返回构造函数的函数。 So to use it, you'd do something like: 因此,要使用它,您需要执行以下操作:

var SomeObject = getLibrary().SomeObject();
// Note --------------------------------^^

...to call it to get back the constructor function it returns. ... 调用它以返回它返回的构造函数。

Then if you use that to create an instance: 然后,如果您使用它来创建实例:

var myInstance = new SomeObject({id:"some_id"});

... instanceof will work correctly: ... instanceof将正常工作:

console.log(myInstance instanceof SomeObject); // true

Live Example: 现场示例:

 var library = {}; function getLibrary() { return library; } getLibrary().SomeObject = function() { function SomeObject(attrs) { this.id = attrs.id; this.description = attrs.description || ''; this.result = { id: this.id, description: this.description, }; } SomeObject.prototype.doSomething = function(actual) { return 'do something'; }; SomeObject.prototype.calculate = function() { return 42; }; return SomeObject; }; var SomeObject = getLibrary().SomeObject(); var myInstance = new SomeObject({id: "myid"}); document.body.innerHTML = myInstance instanceof SomeObject; 

If you want to know what function (probably) created myInstance , provided nothing has messed up SomeObject.prototype (as they routinely do), you can get it from myInstance.constructor . 如果您想知道是什么(可能)函数创建了myInstance ,而没有什么让SomeObject.prototype混乱(就像他们通常所做的那样),则可以从myInstance.constructor获取它。 As of ES2015, you can get the name of that function from its name property, and several browsers have supported that for years even though it's only recently been standardized. 由于ES2015的,你可以从它的获取函数的名称 name属性,和几个浏览器都支持,对于即使它只是在最近被标准化多年。 So (on compliant browsers): 因此(在兼容的浏览器上):

console.log(myInstance.constructor.name); // "SomeObject"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM