I have a simple class with a single method exec(arg1,..,argn)
and I want to have a number of alias methods which call exec
with predefined argument values (e.g. exec_sync = exec.bind(this, true)
).
The following does the trick:
class Executor {
constructor() {
this.exec_sync = this.exec.bind(this, true);
}
exec(sync, cmd, args/* ... */) {
// impl
}
}
But I don't know if this is a good idea or if this is idiomatic to ES6.
UDATE:
In a real-life example I have two nested loops with respectively 3 and 4 loops, which are used to dynamically add a total number of 12 alias methods to the class. It would be a cumbersome task to explicitly define the alias methods when you actually can take advantage of JS being a prototype-based programming language.
UPDATE 2 - EXAMPLE:
Suppose we have have a simple HTTP client with a method request(method, body)
and we want to provide alias methods for GET
, PUT
, etc. It would look something like the following:
class HTTP {
constructor() {
['GET', 'PUT', 'POST', 'DEL'].forEach((method) => {
this[method] = this.request.bind(this, method);
}, this);
}
request(method, body) {
// execute the HTTP request
}
}