When setting up a Socket.IO client, I have multiple methods structured like the following:
myobject.prototype.A = function (callback) {
this.foo('a', null, callback);
}
myobject.prototype.B = function (bar, callback) {
this.foo('b', { bar }, callback);
}
myobject.prototype.C = function (baz, qux, callback) {
this.foo('c', { baz, qux }, callback);
}
The specific details of this.foo
are not important, but it requires 3 parameters: a string, an object created from the method's parameters, and a callback function.
I want to centralize the setup for these methods. My goal is to create something similar to this structure:
// Unsure about the format of args
const methods = {
A: { socketName: 'a', args: [ ] },
B: { socketName: 'b', args: [ 'bar' ] },
C: { socketName: 'c', args: [ 'baz', 'qux' ] }
};
for (let m in methods) {
const mData = methods[m];
this.prototype[m] = function (what_do_I_put_here_?, callback) {
// How do I construct "otherArgs"?
this.foo(mData.socketName, otherArgs, callback);
}
}
I believe utilizing destructuring assignments might be helpful, although I am uncertain on how to implement them in this particular scenario.