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

178
Views
Javascript combining different functions into one

let's say I have a bunch of functions like

_computeValue(value1, value2) {
  //return some stuff
}

_computeIcon(value1, value3) {
  //return some stuff
}

_computeStyle(value2, value5, value1) {
  //return some stuff
}

What I want is to combine all this functions into a single one like this:

_compute(key, parameters){
  if(key === 'style') {
    //return style function
  }
  if(key === 'Icon') {
    //return Icon function
  }
  .....
}

What would be the best way to do it, considering I pass different value and need to use it in the right place..

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

By the looks of your second code block, a switch statement would likely be a good solution. Example:

_compute(key, parameters) {

  switch(key) {
    case 'style':
      // style code
      break;

    case 'icon':
      // icon code
      break;

    ...

    default: // optional
      // none of the above
      break;
}
about 4 years ago · Juan Pablo Isaza Report

0

Add the functions to an object and then call them using the key:

const _compute = {
  style: function fn(...params) {
    return `style: ${params}`;
  },
  icon: function (...params) {
    return `icon ${params}`;
  }
};

console.log(_compute.style(1));
console.log(_compute.icon(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!