Last active
March 8, 2021 04:47
-
-
Save dsasse07/e750908eb15c9d57382aa61b76ca20c4 to your computer and use it in GitHub Desktop.
Storing functions in Objects for Abstract invocation
This file contains hidden or 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
// Using Conditionals | |
const newMessage = {type: "error", message: "You still haven't squished the bugs!"} | |
const generateLog = newMessage => { | |
if (newMessage.type === "warn" { | |
Log.warn(newMessage.message) | |
} else if (newMessage.type === "error" { | |
Log.error(newMessage.message) | |
} else if (newMessage.type === "notify" { | |
Log.notify(newMessage.message) | |
} | |
} | |
generateLog(newMessage) | |
// Without Conditional Statements | |
const newMessage = {type: "error", message: "You still haven't squished the bugs!"} | |
const logGenerator = { | |
warn: Log.warn, | |
notify: Log.notify, | |
error: Log.error | |
} | |
logGenerator[newMessage.type](newMessage.message) | |
// logGenerator[newMessage.type] loads the Log.error function which is then invoked | |
// with the argument of newMessage.message. Any of the Log functions can be called without | |
// any extra conditionals |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment