Currently, I am working with Typescript but presenting this question in Javascript. Any assistance for either language would be greatly appreciated.
I have two objects that share the same interface. My goal is to create a third object with the same interface, where each function calls corresponding methods from both of the objects it contains. Is there a method to generate all these functions programmatically to avoid manually typing them out?
c = console.log; // shorthand
obj1 = function() {}
obj1.prototype = {
foo: (x) => { c("1" + x) },
bar: (x, y) => { c("1" + x + y) }
}
obj2 = function() {}
obj2.prototype = {
foo: (x) => { c("2" + x) },
bar: (x, y) => { c("2" + x + y) }
}
obj3 = function() {
this.o1 = new obj1()
this.o2 = new obj2()
}
obj3.prototype = {
foo: function(x) {
this.o1.foo(x);
this.o2.foo(x);
},
bar: function(x, y) {
this.o1.bar(x, y);
this.o2.bar(x, y);
}
}
I'm seeking a way to define obj3 without the need to individually write each member function due to having quite a few of them.