title: Safely using eval to use variable as an object name
body: <p>As shown in this example </p>
  
  <p><a href="https://stackoverflow.com/questions/6084858/javascript-use-variable-as-object-name">javascript-use-variable-as-object-name</a></p>
  
  <p>I am using eval to use a DOM attribute to select an element from an array. Though there is no direct way for the user to change the input, I want to be as secure as possible and make sure that the variable is indeed an integer before I evaluated it.</p>
  
  <p>Which of the following would be the best, most secure, way?</p>
  
  <pre><code>$(".listitem").click(function(){
      var id = $(this).attr("record-id");
      if(!isNaN(new Number(id))){
          Storage.search.nearby.currec = rowsHolder[eval(id)];
      }else{
          // send email to admin, shut down
      }
  });
  </code></pre>
  
  <p>or</p>
  
  <pre><code>$(".listitem").click(function(){
      var id = $(this).attr("record-id");
      if(parseInt(id)){
          Storage.search.nearby.currec = rowsHolder[eval(id)];
      }else{
          // send email to admin, shut down
      }
  });
  </code></pre>
  
  <p>More, but not required info:</p>
  
  <p>Basically I am pulling down a large JSON string from online, containing an array of records. Upon building a table from the info using a for statement ( <em>for(i in array)</em> ), I push each row into an array called <em>rowsHolder</em> and give the tr an attribute of <em>record-id="i"</em>. Then when the user clicks the row, I call the method you see above. I am using PhoneGap with JQuery Mobile.</p>
  
  <p>As always, thanks for the input</p>
  
  <p>-D</p>