我创建了一个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" 

其他回答

还有另一种技术可以识别类 你可以像下面这样在实例中存储引用到你的类。

class MyClass {
    static myStaticProperty = 'default';
    constructor() {
        this.__class__ = new.target;
        this.showStaticProperty = function() {
            console.log(this.__class__.myStaticProperty);
        }
    }
}

class MyChildClass extends MyClass {
    static myStaticProperty = 'custom';
}

let myClass = new MyClass();
let child = new MyChildClass();

myClass.showStaticProperty(); // default
child.showStaticProperty(); // custom

myClass.__class__ === MyClass; // true
child.__class__ === MyClass; // false
child.__class__ === MyChildClass; // true

问题似乎已经回答了,但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要求的。

对于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…);

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; 
   }   
}

要获得“伪类”,可以通过获取构造函数

obj.constructor

假设在你进行继承时构造函数被正确地设置了——就像这样:

Dog.prototype = new Animal();
Dog.prototype.constructor = Dog;

这两行,加上:

var woofie = new Dog()

会做出伍菲。构造器指向狗。注意,Dog是一个构造函数,是一个function对象。但是你可以这样做,如果。构造函数=== Dog){…}。

如果你想获得类名作为字符串,我发现以下工作良好:

http://blog.magnetiq.com/post/514962277/finding-out-class-names-of-javascript-objects

function getObjectClass(obj) {
    if (obj && obj.constructor && obj.constructor.toString) {
        var arr = obj.constructor.toString().match(
            /function\s*(\w+)/);

        if (arr && arr.length == 2) {
            return arr[1];
        }
    }

    return undefined;
}

它获取构造函数,将其转换为字符串,并提取构造函数的名称。

注意,obj.constructor.name可以很好地工作,但它不是标准的。它支持Chrome和Firefox,但不支持IE,包括IE 9或IE 10 RTM。