Javascript javascript获取类型/实例名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6973866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:03:38  来源:igfitidea点击:

javascript get type/instance name

javascript

提问by Aaron Yodaiken

Is there a reliable way of getting the instance of a JavaScript object?

有没有可靠的方法来获取 JavaScript 对象的实例?

For example, relying on the fake 'obj.getInstance()' function.

例如,依靠假的' obj.getInstance()' 函数。

var T = {
    Q: {
        W: {
           C: function() {}
        }
    } 
};

var x = new T.Q.W.C();
console.log( x.getInstance() === T.Q.W.C); // should output true

If this is not part of the ECMA specification, please include browser/node.js support and compatibility in answers.

如果这不是 ECMA 规范的一部分,请在答案中包含浏览器/node.js 支持和兼容性。

回答by sveilleux2

Only tested on Chrome:

仅在 Chrome 上测试:

function FooBar() {

}

var foobar = new FooBar();

console.log(foobar.constructor.name);  // Prints 'FooBar'

回答by Daniel Baulig

To get a pointer to the instantiating function (which is not a "class", but is the type), use obj.constructorwhere objis any object.

要获得指向实例化函数的指针(它不是“类”,而是类型),请使用obj.constructorwhere objis any object。



In JavaScript there are no classes. As such, there are no class instances in JavaScript. There are only objects. Objects inherit from other objects (their so called prototypes). What you are doing in your code is literally defining an object T, which's attribute Q is another object, which's attribute W is another object, which's attribute C is a function.

在 JavaScript 中没有类。因此,JavaScript 中没有类实例。只有对象。对象继承自其他对象(它们所谓的原型)。您在代码中所做的就是定义一个对象 T,其属性 Q 是另一个对象,其属性 W 是另一个对象,其属性 C 是一个函数。

When you are "creating a new instance of T.Q.W.C", you are actually only calling the function T.Q.W.C as a constructor. A function called as a constructor will return a new object on which the constructor function was called (that is with thisbeeing the new object, like constructorFunction.apply(newObject, arguments);). That returned object will have a hidden property constructorwhich will point to the function that was invoked as a constrcutor to create the object. Additionally there is a language feature which allows you to test if a given function was used as the constructor function for an object using the instanceofoperator.

当您“创建 TQWC 的新实例”时,您实际上只是将函数 TQWC 作为构造函数调用。作为构造函数调用的函数将返回调用构造函数this的新对象(即生成新对象,如constructorFunction.apply(newObject, arguments);)。返回的对象将具有一个隐藏属性constructor,该属性将指向作为构造函数调用以创建对象的函数。此外,还有一个语言功能,它允许您使用instanceof运算符测试给定函数是否用作对象的构造函数。

So you could do the following:

因此,您可以执行以下操作:

console.log(x instanceof T.Q.W.C);

OR

或者

console.log(x.constructor === T.Q.W.C);

回答by Michael van der Kamp

In case anyone is still reading this in 2017:

如果有人在 2017 年仍在阅读本文:

Be careful about using obj.constructor, as it is not an immutable property.

使用时要小心obj.constructor,因为它不是一成不变的属性。

The instanceofoperator is generally more reliable. You can also use Object.getPrototypeOf(obj):

instanceof运营商一般比较可靠。您还可以使用Object.getPrototypeOf(obj)

var arr = [];
arr instanceof Array; // true
Object.getPrototypeOf(arr); // Array [  ]
Object.getPrototypeOf(arr) === Array.prototype; // true

See this useful document on the topic:

请参阅有关该主题的有用文档:

https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch5.md

https://github.com/getify/You-Dont-Know-JS/blob/master/this%20%26%20object%20prototypes/ch5.md

回答by fancyguts

I had some trouble with this, the solution was:

我遇到了一些麻烦,解决方案是:

String(x.constructor) === String(T.Q.W.C)

回答by Peter

You can do :

你可以做 :

    get(){ // Method
        for ( var nameOfVariable in window ) 
            if (eval(nameOfVariable +"=== this")) return nameOfVariable;// true if variable name is instace of this
        return "";            
    }