Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 736885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:39:23+00:00 2026-05-14T07:39:23+00:00

I currently have a Javascript function that uses a string to reference an object

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:39:23+00:00Added an answer on May 14, 2026 at 7:39 am

    If you’re talking about the item1 part, you’re looking for:

    myValue = myTestObject["item1"];
    

    No need for eval. (There almost never is.)

    If you’re talking about getting at the myTestObject variable using a “myTestObject” string, you want to refactor the code so you’re not doing that, rather than using eval. Unfortunately the variable object used for symbol resolution within the function is not accessible directly. The refactor could just use an object explicitly:

    var stringToObjectRef = function() {
    
        var objects = {};
    
        var myTestVar = "myTestObject";
        objects.myTestObject = { 'item1' : 100, 'item2' : 12, 'item4' : 18 };
    
        var myValue = objects[myTestVar].item1;
    
        alert(myValue);
    
    }();
    

    Off-topic, I don’t recall precisely why, but if you’re going to execute that anonymous function immediately like that, you need to put the function expression in parentheses:

    var x = (function() { return 5; })();
    

    rather than

    var x = function() { return 5; }();
    

    Again, I don’t recall why, and whether it was because of an implementation bug in a popular interpreter or an actual requirement of the syntax.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have a web page that uses javascript, however; when I use my
Here is the Javascript I currently have <script type=text/javascript> $(function() { $('.slideshow').hover( function() {
We currently have a tool on our website that is created by JavaScript. The
I am currently trying to pass an array that I have created in Javascript
I'm currently using JavaScript (CommonJS) in Titanium Studio and have a question about prototyping.
I have a HTML page that uses Javascript and it works fine in IE
I'm currently building an JavaScript project that uses NodeJS and some objects are shared
currently im working with a form that on submit uses javascript to validate all
I have a web site I created that uses Python, HTML, and javascript. The
I currently have a layout that uses Jquery UI Drag and Drop.. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.