问题
I have a class, and another class that extends that class.
class Shape {
constructor() {
return this;
}
}
class Circle extends Shape {
constructor() {
super();
return this;
}
}
let foo = new Circle();
I can get foo's class with
let className = foo.constructor.name
// returns string 'Circle'
Is it possible to get the name of foo's superclass ('Shape') in a similar manner?
回答1:
Object.getPrototypeOf(Object.getPrototypeOf(foo)).constructor.name;
回答2:
Got it:
let superClassName = foo.__proto__.__proto__.constructor.name
// return Shape
Edit: this returns the parent class, which is not necessarily the class Circle extended originally. In my use case they're the same, but there's subtle ambiguity about the meaning of 'superclass' here.
回答3:
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"));
回答4:
Your ultimate superclass is always Object, so there's no point trying to derive it. You need to probe whether the instance in question is an instance of something else using the 'is' keyword. I'm writing this from an Actionscript-3 perspective so I'm not sure exactly what the syntax is in ES-6 but it should be along the lines of "if (this is Shape)". That would return true if the instance in question extends the class you're talking about, false otherwise.
However what you're asking is where Shape branches off the standard library or the standard Object, and there is no way the compiler or the runtime could figure that out. In theory it would be awfully nice if ECMA languages could give you a backwards array of inheritance for a given class, but AFAIK that is not and has never been a feature. Without the full chain of inheritance, you need to know what superclass you're looking for or you can't derive it in any logical way.
来源:https://stackoverflow.com/questions/34572580/get-superclass-name-in-es6