I need to dynamically add cases to a switch. I want the user to be able to add items and every item needs it\'s own switch case.
You can use object with callback functions instead:
var callbacks = {};
function add(_case, fn) {
callbacks[_case] = callbacks[_case] || [];
callbacks[_case].push(fn);
}
function pseudoSwitch(value) {
if (callbacks[value]) {
callbacks[value].forEach(function(fn) {
fn();
});
}
}
and you can add new entry using:
add('something', function() {
// case for something
});