我创建了一个JavaScript对象,但如何确定该对象的类?

我想要类似于Java的. getclass()方法的东西。


当前回答

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

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。

其他回答

你可以通过使用constructor属性获取创建对象的构造函数的引用:

function MyObject(){
}

var obj = new MyObject();
obj.constructor; // MyObject

如果你需要在运行时确认对象的类型,你可以使用instanceof操作符:

obj instanceof MyObject // true

为了保持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是一种无类语言:它不像Java那样有静态定义类行为的类。JavaScript使用原型而不是类来定义对象属性,包括方法和继承。用JavaScript中的原型模拟许多基于类的特性是可能的。

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

如果你不仅需要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'