Solved

How to resolve variable that holds name of variable?

Posted on 2015-01-29
3
82 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 500 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

729 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