我创建了一个JavaScript对象,但如何确定该对象的类?
我想要类似于Java的. getclass()方法的东西。
我创建了一个JavaScript对象,但如何确定该对象的类?
我想要类似于Java的. getclass()方法的东西。
当前回答
我发现object.constructor. tostring()在IE中返回[object objectClass],而不是在chome中返回函数objectClass(){}。所以,我认为http://blog.magnetiq.com/post/514962277/finding-out-class-names-of-javascript-objects中的代码在IE中可能不能很好地工作。我修复了代码如下:
代码:
var getObjectClass = function (obj) {
if (obj && obj.constructor && obj.constructor.toString()) {
/*
* for browsers which have name property in the constructor
* of the object,such as chrome
*/
if(obj.constructor.name) {
return obj.constructor.name;
}
var str = obj.constructor.toString();
/*
* executed if the return of object.constructor.toString() is
* "[object objectClass]"
*/
if(str.charAt(0) == '[')
{
var arr = str.match(/\[\w+\s*(\w+)\]/);
} else {
/*
* executed if the return of object.constructor.toString() is
* "function objectClass () {}"
* for IE Firefox
*/
var arr = str.match(/function\s*(\w+)/);
}
if (arr && arr.length == 2) {
return arr[1];
}
}
return undefined;
};
其他回答
为了保持ECMAScript 6一贯的向后兼容性,JavaScript仍然没有类类型(尽管不是每个人都理解这一点)。它确实有一个class关键字作为创建原型的类语法的一部分——但仍然没有称为class的东西。JavaScript现在不是,也从来都不是经典的OOP语言。从类的角度来谈论JS要么是一种误导,要么是一种还没有找到原型继承的迹象(只是保持它的真实)。
这意味着。构造函数仍然是获取构造函数引用的好方法。这个。constructor。prototype是访问原型本身的方法。因为这不是Java,所以它不是一个类。它是实例实例化的原型对象。下面是一个使用ES6语法糖创建原型链的例子:
class Foo {
get foo () {
console.info(this.constructor, this.constructor.name)
return 'foo'
}
}
class Bar extends Foo {
get foo () {
console.info('[THIS]', this.constructor, this.constructor.name, Object.getOwnPropertyNames(this.constructor.prototype))
console.info('[SUPER]', super.constructor, super.constructor.name, Object.getOwnPropertyNames(super.constructor.prototype))
return `${super.foo} + bar`
}
}
const bar = new Bar()
console.dir(bar.foo)
这是使用babel-node输出的结果:
> $ babel-node ./foo.js ⬡ 6.2.0 [±master ●]
[THIS] [Function: Bar] 'Bar' [ 'constructor', 'foo' ]
[SUPER] [Function: Foo] 'Foo' [ 'constructor', 'foo' ]
[Function: Bar] 'Bar'
'foo + bar'
你知道了!2016年,JavaScript中有class关键字,但仍然没有类类型。这一点。构造函数是获取构造函数的最佳方式,this。Constructor。prototype是访问原型本身的最佳方式。
在javascript中,没有类,但我认为您需要构造函数名称和obj.constructor.toString()将告诉您需要什么。
如果你可以访问类Foo的实例(Foo = new Foo()),那么只有一种方法可以从实例中访问类:Foo。Javascript中的构造函数= Java中的foo.getClass()。
eval()是另一种方法,但由于eval()永远不被推荐,它适用于所有事情(类似于Java反射),所以不推荐使用这种方法。foo。构造函数= Foo
getClass()函数使用constructor.prototype.name
我找到了一种方法来访问类,比上面的一些解决方案要干净得多;在这儿。
function getClass(obj) {
// if the type is not an object return the type
if((let type = typeof obj) !== 'object') return type;
//otherwise, access the class using obj.constructor.name
else return obj.constructor.name;
}
它是如何工作的
构造函数有一个名为name access的属性,它将为您提供类名。
更简洁的代码版本:
function getClass(obj) {
// if the type is not an object return the type
let type = typeof obj
if((type !== 'object')) {
return type;
} else { //otherwise, access the class using obj.constructor.name
return obj.constructor.name;
}
}
我建议使用Object.prototype.constructor.name:
Object.defineProperty(Object.prototype, "getClass", {
value: function() {
return this.constructor.name;
}
});
var x = new DOMParser();
console.log(x.getClass()); // `DOMParser'
var y = new Error("");
console.log(y.getClass()); // `Error'