Check if a constructor inherits another in ES6

后端 未结 3 1769
长情又很酷
长情又很酷 2020-11-29 11:58

I have a situation where I need to check if a constructor (X) has another constructor (Y) in its prototype chain (or is Y itself).

The quickest means to do this migh

3条回答
  •  生来不讨喜
    2020-11-29 12:30

    There's also Object.prototype.isPrototypeOf(). Seems like a perfect use case, no?

    Babel

    class A {}
    class B extends A {}
    class C extends B {}
    console.log(C === C)
    console.log(B.isPrototypeOf(C))
    console.log(A.isPrototypeOf(C))
    

提交回复
热议问题