Solved

How to resolve variable that holds name of variable?

Posted on 2015-01-29
3
76 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
  • 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now