Skip to content Skip to sidebar Skip to footer

How To Know The Ancestor-classes Of A Given Class In Javascript/es6

var A = class A {}; var B = class B extends A {}; var C = class C extends B {}; Given the code above assuming I only have access to class 'C', how can I know what are its ancestor

Solution 1:

You can iterate the prototype chain of C.prototype and get the prototype's constructor property.

var A = classA {};
var B = classBextendsA {};
var C = classCextendsB {};

var proto = C.prototype;
while (proto !== Object.prototype) {
  console.log(proto.constructor.name, proto.constructor);
  proto = Object.getPrototypeOf(proto);
} 

Post a Comment for "How To Know The Ancestor-classes Of A Given Class In Javascript/es6"