我创建了一个JavaScript对象,但如何确定该对象的类?
我想要类似于Java的. getclass()方法的东西。
我创建了一个JavaScript对象,但如何确定该对象的类?
我想要类似于Java的. getclass()方法的东西。
当前回答
我现在有一个通用的情况,使用这个:
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"
其他回答
对于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…);
这个getNativeClass()函数对于未定义的值返回“undefined”,对于空值返回“null”。对于所有其他值,CLASSNAME-part从[object CLASSNAME]中提取,这是使用object .prototype. tostring .call(value)的结果。
getAnyClass()的行为与gettnativeclass()相同,但也支持自定义构造函数
function getNativeClass(obj) {
if (typeof obj === "undefined") return "undefined";
if (obj === null) return "null";
return Object.prototype.toString.call(obj).match(/^\[object\s(.*)\]$/)[1];
}
function getAnyClass(obj) {
if (typeof obj === "undefined") return "undefined";
if (obj === null) return "null";
return obj.constructor.name;
}
getClass("") === "String";
getClass(true) === "Boolean";
getClass(0) === "Number";
getClass([]) === "Array";
getClass({}) === "Object";
getClass(null) === "null";
getAnyClass(new (function Foo(){})) === "Foo";
getAnyClass(new class Foo{}) === "Foo";
// etc...
我现在有一个通用的情况,使用这个:
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"
如果你不仅需要GET类,还需要EXTEND类,这样写:
让我们来
class A{
constructor(name){
this.name = name
}
};
const a1 = new A('hello a1');
因此扩展A只使用实例:
const a2 = new (Object.getPrototypeOf(a1)).constructor('hello from a2')
// the analog of const a2 = new A()
console.log(a2.name)//'hello from a2'
问题似乎已经回答了,但OP想要访问和对象的类,就像我们在Java中所做的那样,选择的答案是不够的(imho)。
通过下面的解释,我们可以得到一个对象的类(实际上在javascript中称为prototype)。
var arr = new Array('red', 'green', 'blue');
var arr2 = new Array('white', 'black', 'orange');
你可以像这样添加属性:
Object.defineProperty(arr,'last', {
get: function(){
return this[this.length -1];
}
});
console.log(arr.last) // blue
但是.last属性将只对从Array原型实例化的'arr'对象可用。因此,为了使.last属性对所有从Array prototype实例化的对象可用,我们必须为Array prototype定义.last属性:
Object.defineProperty(Array.prototype,'last', {
get: function(){
return this[this.length -1];
}
});
console.log(arr.last) // blue
console.log(arr2.last) // orange
这里的问题是,你必须知道“arr”和“arr2”变量属于哪种对象类型(原型)!换句话说,如果您不知道'arr'对象的类类型(原型),那么您将无法为它们定义属性。在上面的例子中,我们知道arr是Array对象的实例,这就是为什么我们使用Array。prototype为Array定义一个属性。但如果我们不知道“arr”的类(原型)呢?
Object.defineProperty(arr.__proto__,'last2', {
get: function(){
return this[this.length -1];
}
});
console.log(arr.last) // blue
console.log(arr2.last) // orange
正如你所看到的,在不知道'arr'是一个数组的情况下,我们可以添加一个新属性,只需使用'arr.__proto__'引用'arr'的类即可。
我们访问了'arr'的原型,但不知道它是Array的实例,我认为这是OP要求的。