• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 86
  • Last Modified:

How to resolve variable that holds name of variable?

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
894359
Asked:
894359
  • 2
1 Solution
 
MrunalCommented:
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
 
Robert SchuttSoftware EngineerCommented:
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
 
Robert SchuttSoftware EngineerCommented:
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now