我创建了一个JavaScript对象,但如何确定该对象的类?
我想要类似于Java的. getclass()方法的东西。
我创建了一个JavaScript对象,但如何确定该对象的类?
我想要类似于Java的. getclass()方法的东西。
当前回答
对于ES6中的Javascript类,您可以使用object.constructor。在下面的示例类中,getClass()方法返回你所期望的ES6类:
var Cat = class {
meow() {
console.log("meow!");
}
getClass() {
return this.constructor;
}
}
var fluffy = new Cat();
...
var AlsoCat = fluffy.getClass();
var ruffles = new AlsoCat();
ruffles.meow(); // "meow!"
如果你从getClass方法实例化类,请确保将其括在括号中,例如ruffles = new (fluffy.getClass())(args…);
其他回答
我现在有一个通用的情况,使用这个:
class Test {
// your class definition
}
nameByType = function(type){
return type.prototype["constructor"]["name"];
};
console.log(nameByType(Test));
这是我发现的唯一方法,以获得类名类型输入,如果你没有一个对象的实例。
(用ES2017编写)
点表示法也可以
console.log(Test.prototype.constructor.name); // returns "Test"
为了保持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是访问原型本身的最佳方式。
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'
在JavaScript中没有与Java的getClass()完全对应的方法。这主要是因为JavaScript是基于原型的语言,而Java是基于类的语言。
根据你需要getClass()做什么,JavaScript中有几个选项:
typeof 运算符 obj.constructor 函数。原型,proto.isPrototypeOf
举几个例子:
function Foo() {}
var foo = new Foo();
typeof Foo; // == "function"
typeof foo; // == "object"
foo instanceof Foo; // == true
foo.constructor.name; // == "Foo"
Foo.name // == "Foo"
Foo.prototype.isPrototypeOf(foo); // == true
Foo.prototype.bar = function (x) {return x+x;};
foo.bar(21); // == 42
注意:如果你用Uglify编译你的代码,它会改变非全局类名。为了防止这种情况,Uglify有一个——mangle参数,你可以使用gulp或grunt将其设置为false。