Search Results

Search found 1 results on 1 pages for 'user317362'.

Page 1/1 | 1 

  • Using a string as an object reference

    - by user317362
    I currently have a Javascript function that uses a string to reference an object name and acces its properties. I'm currently using eval() to get the the desired effect which I know is very, very wrong. Here is an example of how I'm currently achieving what I want: var stringToObjectRef = function() { var myTestVar = "myTestObject"; var myTestObject = { 'item1' : 100, 'item2' : 12, 'item4' : 18 }; var myValue = eval(myTestVar + '.item1'); alert(myValue); }(); I've tried using something like [myTestVar].item1, but this returns undefined. What is the correct syntax to achieve this? Thanks in advance.

    Read the article

1