如何testingB是Javascript / Node中的A的“子类”?
给定两个类如下所示:
function A(name) { this.name = name; } A.prototype.sayName = function() { console.log(this.name); } var B = require('some-class'); // B is subclass of A?
有没有一种方法来编程确定B是否是A的子类?
编辑:在我的情况下,B是一个函数和B.prototype
扩展A.prototype
。 B不是new A()
的返回。 B instanceof A
似乎不工作。
检查B
是否是A
的子类(不包括B === A
的情况):
B.prototype instanceof A
检查B
是否是A
的子类(包括B === A
的情况):
B.prototype instanceof A || B === A new B() instanceof A // shorter, but creates an instance of B