In order to set up a Socket.IO client, I have a bunch of methods looking like this:
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 content of this.foo
is unimportant, but it takes 3 parameters: a string, an object built from the calling method parameters, and a callback method.
I'd like to have the methods set up in a single place. I'd like to have something looking like this:
// I'm not sure what form the args should take
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 form "otherArgs" ?
this.foo(mData.socketName, otherArgs, callback);
}
}
I think I'll have to look to destructuring assignments but I'm not sure how to use them in this case.
You could do that utilizing closures and an array function:
If you really need it as member functions you could do:
If it is only about member functions you could get rid of the the arrow function like this:
As I don't know the exact use case, it is hard to come up with a solution that exactly matches the needs. But based on the shown code, you should get an idea how this can be achieved.