Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

207
Views
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 answers
Answer question

0

Use spread operator ... syntax for that:

class Whatever {

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

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!