?
Solved

How to resolve variable that holds name of variable?

Posted on 2015-01-29
3
Medium Priority
?
83 Views
Last Modified: 2015-01-30
I can't figure out how to resolve a variable so that the name of the variable that it holds gets update?
var aa = {}; 
aa.someVar = 'Default Value';
var temp = "aa.someVar"
temp = "New Value":
// which I want to be the same as aa.someVar = "New Value";

Open in new window

0
Comment
Question by:894359
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 22

Expert Comment

by:Mrunal
ID: 40579353
your code is syntactically wrong.
Use below code:

$(function(){
var aa = {};
aa.someVar = 'Default Value';
var temp = "aa.someVar";
temp = "New Value";
// which I want to be the same as aa.someVar = "New Value";
alert(temp);
});

Open in new window


Same is posted here.
0
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 2000 total points
ID: 40579653
I think the idea is to alert the original variable which should show the new value was really put in there, not just replicate the results for this specific situation.

Unfortunately the only way I think this is possible (in the exact situation described) is using eval() which is kind of a big no-no. If you want to know why, just google "eval is evil"...

If you are sure the temp variable contains something that will evaluate to a variable reference that you want to set to the new value you can use:
var aa = {};
aa.someVar = 'Default Value';
var temp = "aa.someVar";
eval(temp + ' = "New Value"');
// which I want to be the same as aa.someVar = "New Value";
alert(aa.someVar);

Open in new window


Another option, if you already know which object you want to change (aa) and actually only need to have the property name (someVar) to be a variable, do it like this (much safer code but will only work if you are in that situation of course):
var aa = {};
aa.someVar = 'Default Value';
var temp = "someVar";
aa[temp] = "New Value";
// which I want to be the same as aa.someVar = "New Value";
alert(aa.someVar);

Open in new window

0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 40579676
PS: another option if you always have a temp variable of type "x.y" and x is a global variable you could use:
window.aa = {}; // for jsFiddle, normally you would use: var aa = {};
$(function(){
aa.someVar = 'Default Value';
var temp = "aa.someVar";
var arraytemp = temp.split('.');
window[arraytemp[0]][arraytemp[1]] = "New Value";
// which I want to be the same as aa.someVar = "New Value";
alert(aa.someVar);
});

Open in new window

A bit more generic (for that "x.y" situation that is ;-) and still better than using eval I guess...

PS2: maybe if you can describe your exact situation it would be possible to advise a completely different approach.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question