JavaScript +获取对象/函数名称(JavaScript + Get object / function name)

编程入门 行业动态 更新时间:2024-10-23 18:28:22
JavaScript +获取对象/函数名称(JavaScript + Get object / function name)

我正在使用JavaScript构建应用程序。 此应用程序与第三方库集成。 该库定义了一个这样的对象:

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; };

我在一个名为myInstance的变量中有一个这个对象的实例。 当我执行console.log(myInstance) ,我在Chrome控制台窗口中看到以下内容:

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

我试图从上面得到“SomeObject”部分。 我试过了:

if (myInstance instanceof SomeObject) { ... }

但是,这没有用。 我基本上试图得到“类型”(或函数?)的名称。 我如何从myInstance获得?

谢谢

I am building an app with 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. When I do a console.log(myInstance), I see the following in the Chrome console window:

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

I am trying to get the "SomeObject" part from above. 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?

Thanks

最满意答案

getLibrary().SomeObject不是构造函数,它是一个返回构造函数的函数。 所以要使用它,你会做类似的事情:

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

... 调用它来获取它返回的构造函数。

然后,如果您使用它来创建一个实例:

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

... instanceof将正常工作:

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

实例:

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; 
  
 

如果你想知道什么函数(可能)创建了myInstance ,只要没有搞砸了SomeObject.prototype (正如他们常规做的那样),你可以从myInstance.constructor获取它。 从ES2015开始,您可以从其name属性中获取该函数的name ,并且多个浏览器已经支持多年,即使它最近才被标准化。 所以(在兼容的浏览器上):

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

getLibrary().SomeObject is not a constructor function, it's a function that returns a constructor function. 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:

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. 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. So (on compliant browsers):

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

更多推荐

本文发布于:2023-08-02 18:38:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   对象   名称   JavaScript   object

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!