Background: I am working on an Angular 1.x project that integrates itself with some AWS services. I have a service for each of the DynamoDB tables that I use that manages basic operations like put, query, get, update, etc.. These services are then imported in a "main" service that adds some more complex functions (like read from a table and insert into another). In the table classes I have both "private" functions (I recognize them because they all start with "_") and public. The general service uses both. Every service has the ES6 class syntax, so my table class will be like the following:
export default class {
constructor(some, var, and, const) {
this.some = some;
this.var = var;
this.and = and;
this.const = const;
...
}
_myPrivateMethod() {
console.log(`I'm private!`);
}
myPublicMethod() {
console.log(`I'm public!`);
}
}
Problem: I want to be able to export only the main service into my Angular controllers, and not all of the "table" service, so my goal is to implement a multiple class inheritance, where my main service inherits from all the table services and exposes all of their methods (public and private, I don't care), so that into my controller I import just my main service and I have everything I need.
Some solutions I've found: I read some similar questions here, but none of the answers could fit my case, since I need to pass different vars into my table services' constructors. I'd like to not use any external library that makes easier multiple inheritance, because the project is going to be big and I don't want to keep adding stuff, so I'd like to implement it myself.
The best idea that I've got consist in creating the instances of the child classes, getting their prototypes with Object.getPrototypeOf, and setting the prototype of this
with Object.setPrototypeOf with all them via Object.assign, like this:
import TableClass1 from './tables/1';
import TableClass2 from './tables/2';
export default class MyMainClass {
constructor(some, var, other, param) {
let myTableInstance1 = new TableClass1(some, var);
let myTableInstance2 = new TableClass2(other, param);
let tmpMultiInherit = Object.assign({}, Object.getPrototypeOf(myTableInstance1), Object.getPrototypeOf(myTableInstance2));
Object.setPrototypeOf(this, tmpMultiInherit);
}
}
This seems a good idea to me, because the getPrototypeOf(s) functions returns me exactly what I need to add to my class in the typeof object
, but the Object.assign
line returns {}
, and I cannot understand why.
This question was the most useful I've found, but none of the answers explain how can you implement multi inheritance with different parent classes' constructors' forms.
So if any of you knows why this method fails or has had this problem in the past, I'd be most grateful for your help.
Thank you in advance!