1

Please have a look on the following code -

var abc_text = "Hello";
var def_text = "world";

function testMe(elem) {
    var xyz = elem+"_text";
    alert(xyz);
}

testMe("abc");
testMe("def");

I am trying to pass prefix to a function and the try to print some pre-defined values by concatenating. But the above example just prints "abc_text" and "def_text" .. instead of "Hello" and "world". How can I get it working?

Thank you.

EDIT

I am using Jquery.

TigerTiger
  • 10,590
  • 15
  • 57
  • 72

4 Answers4

6

in this case use

var xyz = window[elem+"_text"];
Jonathan Fingland
  • 56,385
  • 11
  • 85
  • 79
  • This (or alternately eval) is right. But wbdvlpr, note that it's rarely necessary to do this in well-written code. – Matthew Flaschen Jun 14 '09 at 15:29
  • eval is evil. eval should only ever be used in the rarest of situations. It is far too easy to get code injected from other sources. – Jonathan Fingland Jun 14 '09 at 15:34
  • @Jonathan: uh... well, no. It's only easy if you're accepting input from untrusted sources and then blindly passing it to eval(). But it's slow and in this case unnecessary; surely that's reason enough! – Shog9 Jun 14 '09 at 17:40
  • @Jonathan Fingland - yes, it works. But associative array seems better to me. Thanks. – TigerTiger Jun 14 '09 at 19:32
  • @wbdvlpr, oh I agree. the question as asked is solved by the window[] method, but the associative array method is better practice overall – Jonathan Fingland Jun 14 '09 at 22:09
  • @Matthew, @Shog9: From Mozilla: https://developer.mozilla.org/En/Core_JavaScript_1.5_Reference/Global_Functions/Eval#Don%27t_use_eval! , quote: "Don't use eval!". Also see: http://adblockplus.org/blog/five-wrong-reasons-to-use-eval-in-an-extension . for well-intentioned, but really bad advice see http://24ways.org/2005/dont-be-eval . The MDC page makes it clearest : don't use eval if the code *could* be affected by a malicious 3rd party. This doesn't just include user input. this also includes the inclusion of 3rd party scripts on your page, xhr responses that aren't very well sanitized, etc. – Jonathan Fingland Jun 14 '09 at 22:25
  • @Jonathan: yeah, yeah... I know all about eval's bad rep. Point is, there's no code injection risk in the example being discussed here. I hate seeing fear-mongering used in place of facts; if you don't want to go off on a tangent, then just say "use of eval() is generally discouraged, and is not required for this task" and leave it at that. BTW: if you're including untrusted 3rd-party scripts on your page, then you're already doomed - they can call eval() or any other function... – Shog9 Jun 15 '09 at 13:37
  • @shog9. totally agree on that last point 3rd party scripts are generally a bad plan. Obviously in some cases, e.g. google maps, it's really unavoidable. My point, however, was that eval should be your last resort and most certainly not your first. you can call that fear mongering, or you can call it pragmatism. – Jonathan Fingland Jun 15 '09 at 15:18
4

You can eval xyz, but it's better to store abc_text and def_text in associative array or in object;


var text = {"abc" : "Hello", "def" : "Word"};
erenon
  • 18,838
  • 2
  • 61
  • 93
3
var test={"abc":"Hello", "def":"World"};

function testMe(elem) { 
    var xyz = test[elem];
    alert(xyz); 
} 

testMe("abc"); 
testMe("def");

Amar
  • 353
  • 1
  • 2
  • 13
1

There is a pretty good write-up on Dynamic Variables in JavaScript here:

http://www.hiteshagrawal.com/javascript/dynamic-variables-in-javascript

Eli
  • 97,462
  • 20
  • 76
  • 81