在ES6中,如何检查对象的类? [英] In ES6, how do you check the class of an object?
本文介绍了在ES6中,如何检查对象的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在ES6中,如果我创建一个类并创建该类的对象,那么我如何检查对象是否是该类?
In the ES6, if I make a class and create an object of that class, how do I check that the object is that class?
我不能只需使用 typeof
,因为对象仍然是object
。我只是比较构造函数?
I can't just use typeof
because the objects are still "object"
. Do I just compare constructors?
示例:
class Person {
constructor() {}
}
var person = new Person();
if ( /* what do I put here to check if person is a Person? */ ) {
// do stuff
}
推荐答案
你不能做 person instanceof Person
?
单独比较构造函数不适用于子类
Comparing constructors alone won't work for subclasses
这篇关于在ES6中,如何检查对象的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文