i know this function gets a string of valid JavaScript expression or statements and evaluate the expression or execute the statements, but why we need to pass code to a function for execution instead of simply writing the code in the script?
i know this function gets a string of valid JavaScript expression or statements and
Share
Let’s say you have functions:
makeCheckbox(),makeRadio()andmakeSelect()(theoretically). You want to call function by user’s activity (for example menu click). You can make it withif,else ifand so on, you can makeswitch, but you canevaltoo and make something like:where
userChooseis one of the options:radio,checkboxorselect, which is first-char-uppercased first and then concatenated withmake(before) and()(after), which at the end calls one of the functions.This is not good practice because in large projects you can’t simply find callings to function, because they’re “hidden behind”
eval, but sometimes it’s the only way.