I want to add the same function twice to a Javascript class with two different function names.
So far I have come up with two ways how this can be implemented.
- Add function to prototype under a different name:
class MyClass {
computeN() {
// do some computation here
}
}
MyClass.prototype.computeM = MyClass.prototype.computeN;
- Add two functions. Call first from second:
class MyClass {
computeN() {
// do some computation here
}
computeM() {
return computeN();
}
}
The first solutions is no plain class syntax, and the second works, but I would rather have jsut one function call than a nested one. Also, my second solution may be harder to maintain if the naumber of arguments changes in the original function.
Is there a better solution for my little problem?
The motivation is that I am implementing a parser, where different specs use different names for the same data type (uint16, ushort, card16). So having alternative function names for processing these data types would make it easier to follow the different specs.