Check if object is a 'direct instance' of a class

前端 未结 6 815
梦毁少年i
梦毁少年i 2021-01-04 13:26

I have two classes:

class Bar extends Foo { // Foo isn\'t relevant
  constructor(value) {
    if (!(value instanceof Foo)) throw \"InvalidArgumentException:          


        
6条回答
  •  醉话见心
    2021-01-04 14:21

    I coined the function for checking relationships between DOM Classes and elements

    const getCreator = instance => Object.getPrototypeOf(instance).constructor.name;
    // usage
    getCreator(document.documentElement) === "HTMLHtmlElement;
    

提交回复
热议问题