我遇到了问题,我不知道如何从第三类扩展。所以我真的需要用参数"TYPE"调用 A 类,用 C 扩展,并且能够用类 C 调用 getType((。
const TYPE = 'TYPE'
class A {
constructor(type) {
this.type = type;
}
getType() {
return this.type;
}
}
class B {
constructor(id) {
this.id = id;
}
getId() {
return this.id;
}
}
class C extends B {
constructor(id) {
super(id);
//Here should be a function that should bind the method from class A
}
}
const c = new C(1);
console.log(c.getId())
console.log(c.getType())
const TYPE = 'TYPE'
class A {
constructor(type) {
this.type = type;
}
getType() {
return this.type;
}
extend(extendedClassInstance){
extendedClassInstance.type = this.type;
extendedClassInstance.getType = this.getType.bind(extendedClassInstance)
}
}
class B {
constructor(id) {
this.id = id;
}
getId() {
return this.id;
}
}
class C extends B {
constructor(id) {
super(id);
(new A(TYPE)).extend(this)
}
}
const c = new C(1);
console.log(c.getId())
console.log(c.getType())