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
}
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
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
}
.....
}
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
What would be the best way to do it, considering I pass different value and need to use it in the right place..
CodePudding user response:
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;
}
CodePudding user response:
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));
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>