Get superclass name in ES6

I have a class and another class that extends this class.

class Shape { constructor() { return this; } } class Circle extends Shape { constructor() { super(); return this; } } let foo = new Circle(); 

I can get the foo class with

 let className = foo.constructor.name // returns string 'Circle' 

Is it possible to get the superclass name foo ('Shape') in a similar way?

+11
source share
4 answers
 Object.getPrototypeOf(Object.getPrototypeOf(foo)).constructor.name; 
+15
source

Got this:

 let superClassName = foo.__proto__.__proto__.constructor.name // return Shape 

Edit: this returns a parent class that does not necessarily belong to the Circle class that was originally executed. In my use case, they are the same, but there is a subtle ambiguity regarding the meaning of "superclass".

+4
source

You can 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 you define extend this class.

Now, if you want to check whether your object extends a specific class, you can call it as an extends method and pass the name of the parent class as an argument. The extends method will go through the entire inheritance tree and check if your object extends the given class, checking not only its parents, but also its grandparents and grandparents, etc.

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")); 

+1
source

Your final superclass is always Object, so it makes no sense to try to extract it. You need to find out if the instance in question is an instance of something else using the 'is' keyword. I am writing this from the perspective of ActionScript-3, so I'm not sure what the syntax is in ES-6, but it should be in the lines of "if (this Shape)". This will return true if the instance in question extends the class you are talking about, false otherwise.

However, what you are asking for is where the Shape is separated from the standard library or standard object, and the compiler or runtime cannot figure it out. Theoretically, it would be nice if ECMA languages โ€‹โ€‹could give you a backward inheritance array for a given class, but AFAIK, which is not and has never been a function. Without a complete chain of inheritance, you need to know which superclass you are looking for, or you cannot get it in any logical way.

-2
source

Source: https://habr.com/ru/post/1239701/


All Articles