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

前端 未结 5 550
别那么骄傲
别那么骄傲 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:46

    I do not think Simon meant B.prototype = new A() in his question, because this is certainly not the way to chain prototypes in JavaScript.

    Assuming B extends A, use Object.prototype.isPrototypeOf.call(A.prototype, B.prototype)

提交回复
热议问题