- What is the use of Dynamic variables? Any scenarios?
Can I attach this value to a TextField?
var data = "testVariable"; eval("var temp_" + data + "=123;"); alert(temp_testVariable);
Asked
Active
Viewed 181 times
1
Ates Goral
- 132,912
- 25
- 133
- 189
John Cooper
- 6,915
- 27
- 75
- 99
-
3None. Use hash maps (objects). – Jun 14 '11 at 12:39
-
3Why not use the slightly more acceptable `window['temp_' + data] = '123';`? – josh.trow Jun 14 '11 at 12:42
-
@josh.trow I'd say that that's a lot more than "slightly" more acceptable :-) – Pointy Jun 14 '11 at 13:01
-
@Pointy: A mere technicality :) – josh.trow Jun 14 '11 at 13:09
-
http://stackoverflow.com/questions/35221108/how-to-get-random-variables-value-in-javascript/35250618#35250618 – Niklesh Raut Feb 07 '16 at 09:17
2 Answers
6
What is the use of Dyanmic variables? Any Scenarious
For when people haven't heard of objects or arrays.
Can i attach this value to a TextField?
Yes.
Quentin
- 857,932
- 118
- 1,152
- 1,264
6
The answer to 2) is 'yes you can'.
However for 1), and overall, you probably shouldn't be using eval: Why is using the JavaScript eval function a bad idea?
As another comment and the currently upvoted answer says - either use arrays or a map.
Community
- 1
- 1
Andras Zoltan
- 41,423
- 12
- 100
- 159