Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

202
Visualizações
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 Respostas
Responde à pergunta

0

Use spread operator ... syntax for that:

class Whatever {

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

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda