user606521
user606521

Reputation: 15434

How to test if class instance is instance of class that it extends?

class A
class B extends A

b = new B
b instanceof B # true
b instanceof A # false

How I can check that b is "also" instance of A (because B extends A)

Upvotes: 1

Views: 81

Answers (1)

Raza Hussain
Raza Hussain

Reputation: 762

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

You can simply use:

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" 

Here is the code snipt: http://repl.it/meN/1

Upvotes: 2

Related Questions