Last active
December 10, 2022 02:27
-
-
Save sunnyy02/f7636f10045894d4afd302332aff4b3c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// many ifs | |
function performActions(actionName: string) { | |
if (actionName === "save") { | |
// save... | |
} else if (actionName === "update") { | |
// update ... | |
} else if (actionName === "delete") { | |
// delete... | |
} | |
} | |
// refactor with table driven methods | |
const funcs: Record<string, () => void> = { | |
save: () => { | |
// save ... | |
}, | |
update: () => { | |
// update ... | |
}, | |
delete: () => { | |
//delete ... | |
} | |
}; | |
function performActions2(actionName: string) { | |
const action = funcs[actionName]; | |
if (action) { | |
action(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment