我有三个类,其中两个扩展了第三个类。是否有可能以某种方式(不同于手动)将扩展类的名称分配给超级构造函数?在构造函数中保存扩展类的'方法'名称
class Master {
constructor(options) {
this.methods = Object.getOwnPropertyNames(Master.prototype);
}
getMethods() {
return Object.getOwnPropertyNames(Master.prototype);
}
}
class SlaveOne extends Master {
constructor(options) {
super(options);
}
methodOne() {}
methodTwo() {}
}
class SlaveTwo extends Master {
constructor(options) {
super(options);
}
methodThree() {}
methodFour() {}
}
所以,我要的是有两种方法,或在大师班this.methods
分配,这将:
- 回报
['constructor', 'methodOne', 'methodTwo']
叫上SlaveOne
实例时; - 返回
['constructor', 'methodThree', 'methodFour']
当在SlaveTwo
的实例上调用时;或者叫做对SlaveOne
,SlaveTwo
或Master
实例时
我当前的代码将返回相同['constructor', 'getMethods']
。有任何想法吗?
刚刚获得this'对象的'所有方法,它继承的原型。 – Bergi