I created a JavaScript object, but how I can determine the class of that object?
I want something similar to Java\'s .getClass() method.
getClass() and getInstance()You are able to get a reference for an Object's class using this.constructor.
function A() {
this.getClass = function() {
return this.constructor;
}
this.getNewInstance = function() {
return new this.constructor;
}
}
var a = new A();
console.log(a.getClass()); // function A { // etc... }
// you can even:
var b = new (a.getClass());
console.log(b instanceof A); // true
var c = a.getNewInstance();
console.log(c instanceof A); // true
function A() {};
A.getClass = function() {
return this;
}
A.getInstance() {
return new this;
}