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

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

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 Relatório

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