In ES6, how do you check the class of an object?

后端 未结 2 1361
Happy的楠姐
Happy的楠姐 2020-12-29 18:48

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?

I can\'t just use typeof because the obj

相关标签:
2条回答
  • 2020-12-29 19:20

    Can't you do person instanceof Person?

    Comparing constructors alone won't work for subclasses

    0 讨论(0)
  • 2020-12-29 19:24

    Just a word of caution, the use of instanceof seems prone to failure for literals of built-in JS classes (e.g. String, Number, etc). In these cases it might be safer to use typeof as follows:

    typeof("foo") === "string";

    Refer to this thread for more info.

    0 讨论(0)
提交回复
热议问题