How to check if a Javascript Class inherits another (without creating an obj)?

前端 未结 5 546
别那么骄傲
别那么骄傲 2020-11-29 03:12

Eg:

function A(){}
function B(){}
B.prototype = new A();

How can I check if the class B inherits class A?

5条回答
  •  爱一瞬间的悲伤
    2020-11-29 03:42

    You can test direct inheritance with

    B.prototype.constructor === A
    

    To test indirect inheritance, you may use:

    B.prototype instanceof A
    

    (this second solution was first given by Nirvana Tikku)

提交回复
热议问题