I have some code in an IIFE. I have the function getValue()
which will dynamically return the value of the variable that is declared within the IIFE. I am looking for an alternative solution to using eval()
without re-organizing the data structure of the variables.
Note - I am trying to avoid putting my variables inside an object (ie - fruit).
(function() {
var apple = 'Apples!!',
banana = 'Bananas!!',
cucumber = 'Cucumbers!!';
function getValue(key) {
return eval(key); //can I avoid using eval()?
}
console.log(getValue('apple')); //Apples!!
})();