I'm actually dealing with this in Typescript, but I'll write this question in Javascript. An answer for either would be great.
I have two objects that essentially have the same interface. I'd like to create a 3rd object that has the same interface, and each function would just call the methods of the two objects in contains. Is there a way to programmatically create all the functions so I don't have to type them all 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 looking for a way to write obj3 without having to manually write out each of the member functions, since I have a good number of them.