0

Using jsbin.com tool to test Javascript code I used eval() function, but an warning from JSBin says "Eval is Evil". I did some searches about the use of eval() function and some sources says that is always possible avoid of use eval().

My code need access an array using parameters from other array. See this code:

var Collection = {
                  "cell"  : "value[3]", 
                  "lines" : "source[1..7]", 
                  "title" : "greeting"
                 };

var Data = {
            source : ["a", "b", "c"],

            value   : [
                       "done",
                       "easy", 
                       "f", 
                       "g", 
                       "h", 
                       "i", 
                       "j" 
                      ],

            greeting : "Hi!"         

       };

function Process()
 {
  var x = "cell";

  console.log("value[4] = " + Data.value[4]);
  console.log("C : " + Collection[x]);
  console.log("value['cell'] = " + eval("Data." + Collection[x]));
 }

The output is

value[4] = h
C : value[3]
value['cell'] = g

The recomendations says to access using dot notation, like I do in access to value[4], but the value to be accessed will go in a variable, like 'x', in this case with content "cell". This value is used to access Collection array, and select the value that will be used and Data array. I solve it using eval() function.

Are there a way to do this without eval() ??

4

0 回答 0