简体   繁体   中英

Extend 2 classes in one class of 'this' in javascript

i met the problem, that i do not how extend from the third class.. So i really need the way to call the A class with parameter 'TYPE', extend with C, and be able call getType() with class C. Any solutions?

 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()) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM