You could define the following class:
class MyObject
{
extends(className)
{
let next = Object.getPrototypeOf(this);
while(next.constructor.name !== "Object")
{
if(next.constructor.name === className)
return true;
next = Object.getPrototypeOf(next);
}
return false;
}
}
Let each class defined by you extend that class.
Now if you want to check if your object extends a specific class, you can call it's extends method and pass the name of the parent class as argument. The extends method will walk up the whole inheritance tree and check if your object extends the given class checking not just his parents, but also his grand parents and grand grand parents and so on.
Example:
class MyObject
{
extends(className)
{
let next = Object.getPrototypeOf(this);
while(next.constructor.name !== "Object")
{
if(next.constructor.name === className)
return true;
next = Object.getPrototypeOf(next);
}
return false;
}
}
class Vehicle extends MyObject
{
}
class Car extends Vehicle
{
}
class Bmw extends Car
{
}
let car = new Car();
let bmw = new Bmw();
console.log("bmw extends Bmw: " + bmw.extends("Bmw"));
console.log("bmw extends Car: " + bmw.extends("Car"));
console.log("bmw extends Vehicle: " + bmw.extends("Vehicle"));
console.log("car extends Car: " + car.extends("Car"));
console.log("car extends Bmw: " + car.extends("Bmw"));