Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

201
Vistas
How to avoid repeating a long list of parameters/argument in function calls, and have them all defined in one place?

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).

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use spread operator ... syntax for that:

class Whatever {

  constructor() {
    this.someLongInitialTask(...arguments);
  }
about 4 years ago · Juan Pablo Isaza Denunciar

0

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);

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda