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

179
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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