class A
class B extends A
b = new B
b instanceof B # true
b instanceof A # false
我如何检查b
是"还是" A
的实例(因为B
扩展了A
)
答案 0 :(得分:2)
class A
class B extends A
b = new B
console.log "b is an instance of extended class " if b instanceof B # b is an instance of extended class
console.log "b is an instance of extended class A because B extends a " if b instanceof A # b is an instance of extended class A because B extends a
您可以简单地使用:
if b instanceof B && b instanceof A #B is instance of A and B classes
console.log "B is instance of A and B classes"
以下是代码片段: http://repl.it/meN/1