Consider this example:
class Point {}
class Point2D extends Point {}
class Point3D extends Point2D {}
class FloatPoint extends Point {}
class Bar {}
class Foo extends Bar {}
const result = joinWithClosestAncestor([Point, Point2D, Point3D, FloatPoint, Bar, Foo])
// result === [[Point], [Point2D, Point], [Point3D, Point2D], [FloatPoint, Point], [Bar], [Foo, Bar]];
I'm trying to create joinWithClosestAncestor
which takes an array of classes and returns an array of pairs where the first element is the class & the second is the closest ancestor of the class (if any) from the input classes.
In the example Point3D
is derived from Point
through Point2D
, but because Point2D
is in the input list & is closer related to Point3D
than Point
, it is instead chosen. I can use Class.prototype instanceof X
to determine if it's related or not but I can't compare the depth
of the relation.
Is there a way to somehow compare the depth
of instanceof
between different classes to always get the CLOSEST relation? Or any other way?