0

I have seen on some other answers, regarding different questions, to try and avoid using eval() in javascript.

Would using eval() to call an object method cause an issue? It seems to work fine!

Basically, I have along the lines of the following:

var object1 = {
    left: "this is left",
    right: "this is right"
}

I have a list of objects in a dropdown and depending on which is selected I want to call the "left" or "right" property of that object.

So I get the object name by using:

var objName = document.getElementById("objects").options[document.getElementById("objects").selectedIndex].text.toLowerCase(); 
//returning, say "object1" from the list

Then I want to use this variable to call up the property of that object. The problem I faced is the variable is a string and was returning undefined for the property ("objName.left is not a function"). However, I found using eval() returned the property correctly, basically:

objName.left; //returns undefined
eval(objName).left; //returns "this is left"

Like I say, this works fine but I have come across a few answer saying to avoid eval(). So is there an alternative way to do this or will this be fine?

I'm still new to this (as you can probably tell) so thanks in advance for any answers, advice and help with this query!

Mike
  • 254
  • 1
  • 6
  • 16
  • Maybe this can help: http://stackoverflow.com/questions/5613834/convert-string-to-variable-name-in-javascript – mwoelk Aug 31 '15 at 16:39
  • use bracket notation instead [objName].left always avoid using eval() – Leo Javier Aug 31 '15 at 16:41
  • Don’t be fooled by knee-jerk cautioning of `eval()`. While there’s always an alternative that might be better, there’s also cases where `eval()` is perfectly valid as long as you make sure it’s not an attack vector nor a perfomance trap. – dakab Aug 31 '15 at 16:41
  • Wgy don't you use `var objs = document.getElementById("objects"); var val = objs.getElementsByTagName("option")[objs.selectedIndex].innerHTML.toLowerCase()`? – klenium Aug 31 '15 at 16:43
  • Sorry, I misunderstood what your problem is. Howver, I wouldn't use that. You can make a list, validate the input, then access the value as `allowedNames[variable]` where `variable = "object1"`. – klenium Aug 31 '15 at 16:46

1 Answers1

3

If object1 is defined in global scope. You can use Bracket notaion

window[objName].left;

window.object1 = {
    left: "this is left",
    right: "this is right"
}

var objName = "object1";
alert(window[objName].left);

However I would recommend you to use master object, then you can easily access it using bracket notation, even if its not defined in global scope.

var master = {
    object1: {
        left: "this is left",
        right: "this is right"
    }
}

var objName = "object1";
alert(master[objName].left);
Satpal
  • 132,252
  • 13
  • 159
  • 168
  • 2
    this will not work. You should add `window`: `window[objName].left` – just-boris Aug 31 '15 at 16:40
  • And even with `window` it only works for globals. – T.J. Crowder Aug 31 '15 at 16:41
  • It's not defined in the global scope and the master object works perfectly. Thanks for this. Just curious now but what is the problem with using eval()? Or is there not one? – Mike Sep 01 '15 at 07:44
  • @Mike, Just go through http://stackoverflow.com/questions/86513/why-is-using-the-javascript-eval-function-a-bad-idea and https://www.nczonline.net/blog/2013/06/25/eval-isnt-evil-just-misunderstood/ – Satpal Sep 01 '15 at 07:49
  • Thanks, will have a look at them. – Mike Sep 01 '15 at 07:55