我正在使用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: ObjectI 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); // trueLive 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"
更多推荐
发布评论