I want to inherit all the methods from source classes into my main target class/function. I kind of did something, but I wonder if there are better or gentler ways to do this.
The idea is that I can keep good readability and separate methods in groups (files) so I know what belongs where.
P.S. Sorry for my bad english
Here's how I did it:
function Main(){
const self = this
self.name = 'Main'
self.speak = () => {
console.log(`called in class Main by class ${this.name}`)
}
}
class A{
//fake variables for IDE autofill
//no constructor needed
speakA(){
console.log(`called in class A by class ${this.name}`)
}
}
class B{
speakB(){
console.log(`called in class B by class ${this.name}`)
}
}
class C{
speakC(){
console.log(`called in class C by class ${this.name}`)
}
}
;(function assignOFunctionsToObject(target, ...sources){
sources.forEach(source => {
Object.getOwnPropertyNames(source.prototype).forEach(name => {
if(typeof source.prototype[name] === "function") {
target.prototype[name] = source.prototype[name]
}
})
})
})(Main,
A, B, C)
let main = new Main()
main.speak()
main.speakA()
main.speakB()
main.speakC()