const method1 = () => console.log('Does something');
const method2 = () => console.log('Does another thing');
const method3 = () => console.log('Does entirely other thing');
const switchObj = {
a: method1,
b: method2,
c: method3
};
const init = (option) => {
console.log('instead of switch case, use an object');
console.log(switchObj[option]());
}
init('a');
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter