In the C++ world, we often have this kind of API design pattern:
bool foo(int input1, int input2, int& output1, int& output2, ClassA& outputObj);
The function has a set of input parameters as well as output parameters. The return value indicates if the function succeeds or fails.
An example of this API is:
int input1=0;
int input2=1;
int output1=0;
int output2=1;
ClassA a = new ClassA(...);
bool r = foo(input1, input2, output1, output2, a);
However, in the JavaScript, we have no simple way to simulate "change the primitive argument by reference" behavior. If we wrap the primitive type (number
, string
etc.) using objects. The API will be ugly.
Any idea about how to design an elegant API using JavaScript with input parameters, output parameters, return values? There might be objects and primitive types in the parameters.