0

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!!

})();
Jon
  • 8,205
  • 25
  • 87
  • 146
  • 1
    Don't use variables in the first place. Use an object to map names to values. – Barmar Mar 31 '16 at 00:42
  • @Barmar Do you mean putting my variables inside another object (ie - fruit)? I am trying to avoid changing the data structure of my variables. – Jon Mar 31 '16 at 00:44
  • Yes, that's what I mean. What you're trying to do is simply the wrong way to do it. Variable names are for the programmer, they shouldn't be part of the application data. – Barmar Mar 31 '16 at 00:46
  • See: http://stackoverflow.com/questions/598878/how-can-i-access-local-scope-dynamically-in-javascript – Jack A. Mar 31 '16 at 11:50

3 Answers3

4

How about this?

(function() {

    var fruit = {
      apple: 'Apples!!',
      banana: 'Bananas!!',
      cucumber: 'Cucumbers!!'
    };

    function getValue(key) {
        return fruit[key]; //can I avoid using eval()?
    }

    console.log(getValue('apple')); //Apples!!

})();
Jack A.
  • 4,245
  • 1
  • 20
  • 34
  • I want to avoid the data structure of my variables. Can I avoid putting the variables inside fruit? – Jon Mar 31 '16 at 00:43
  • @Jon This is how one normally does what you're asking for in JavaScript. Is there a particular reason you don't want to use a hash? – Jack A. Mar 31 '16 at 02:26
  • @Jon - There is no way to access plain local variables by string name other than with `eval()`. Javascript does not support any other way to directly access them by name. So, you either put them into an object where you can access them by property name or you use `eval()`. Those are the choices. – jfriend00 Mar 31 '16 at 07:43
1
(function() {

    var fruit = {
        apple: 'Apples!!',
        banana: 'Bananas!!',
        cucumber: 'Cucumbers!!';
    }
    function getValue(key) {
        return fruit[key]; //can I avoid using eval()?
    }

    console.log(getValue('apple')); //Apples!!

})();
Wainage
  • 4,892
  • 3
  • 12
  • 22
  • I want to avoid the data structure of my variables. Can I avoid putting the variables inside fruit? – Jon Mar 31 '16 at 00:43
  • No. You need an object to make this work. Is there a reason you don't want an object? – Wainage Mar 31 '16 at 00:44
1

It's possible if you enumerate and copy all valid variable names:

(function() {

    var apple = 'Apples!!',
        banana = 'Bananas!!',
        cucumber = 'Cucumbers!!';

    function getValue(key) {
        var vals = { apple: apple, banana: banana, cucumber: cucumber};
        return vals[ key];
    }

    console.log(getValue('apple')); //Apples!!

})();

You could also consider sanitizing the use of eval by validating parameter values:

(function() {

    var apple = 'Apples!!',
        banana = 'Bananas!!',
        cucumber = 'Cucumbers!!';

    var publicVars = ["apple", "banana", "cucumber"];

    function getValue(key) {
        if(publicVars.indexOf(key) < 0)
            throw new Error("Invalid variable name for getValue: " + key);
        return eval(key);
    }


    console.log(getValue('apple')); //Apples!!

})();


Footnote: examples above are aimed at minimizing the client cost of program maintenance. Designing a new project using them, as opposed to the suggested 'fruit' object, is not suggested.
traktor
  • 17,588
  • 4
  • 32
  • 53