I have the following (example pseudocode) class:
class Whatever {
constructor(param1, param2, ... , param15) {
this.someLongInitialTask1(param1, param2, ... , param15);
this.someLongInitialTask2(param1, param2, ... , param15);
// some other operations
}
someLongInitialTask1(param1, param2, ..., param15) {
//here some operation to be performed only at inizialization
}
someLongInitialTask2(param1, param2, ..., param15) {
//here some operation to be performed only at inizialization
}
}
I would like to be able to specify in my code the list of parameters/arguments param1, param2, ..., param15 only in 1 place, instead of repeating them every time I need them. The reason is to make more robust and maintainable code and avoid forgetting to change the parameters in the various multiple calls.
How is it possible to do that?
I do not want these parameters to be some field/property of the class because they are useful only when the class is initially instantiated and there is no need to "memorize" them within the object (no need to assign them to fields of the object).
Having lots of parameters is something I tend to avoid these days (much prefer object destructured params) but if you just want to pass args down, you can use the spread syntax.
class Whatever {
constructor(...params) {
this.someLongInitialTask(...params);
}
someLongInitialTask(...params) {
const [param1] = params;
console.log(param1);
this.someLongInitialTask2(...params);
}
someLongInitialTask2(...params) {
const [param1, param2, param3] = params;
console.log(param1, param2, param3);
}
}
new Whatever(1,2,3);