Reputation: 1959
Having a structure like this or similar with multiple levels of inheritance:
class Abstract {};
class Concrete extends Abstract {};
class MoreSpecificConcrete extends Concrete {};
class EvenMoreSpecificConcrete extends MoreSpecificConcrete {};
I want to know the last class before Function
that EvenMoreSpecificConcrete
inherits from.
Upvotes: 3
Views: 47
Reputation: 1959
You can recursively go through the prototype chain checking if you ended up at the Function prototype (which every Function extends) and return the last element before this.
function getBaseClass(MaybeClass) {
if (typeof MaybeClass !== 'function') return;
const Proto = Reflect.getPrototypeOf(MaybeClass);
return (Proto === Function.prototype || Proto === null)
? MaybeClass
: getBaseClass(Proto);
}
You can then compare the returned class with the Abstract:
getBaseClass(EvenMoreSpecificConcrete) === Abstract;
Fiddle: https://jsfiddle.net/9nLuvyob/
Upvotes: 3