5 Replies - 319 Views - Last Post: 14 March 2012 - 02:46 PM

#1 Asperant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 11

Name of a variable

Posted 13 March 2012 - 12:09 PM

There are two variables:
var index1=5;
var index2=7;



And I need start working with one of them by next way:

someFunction(index."1"); //Or something like this



i.e. - catenation of variable name

My question:

Does such possibility exist in Javascript, or not? And what syntax? I've been looking for, but possibly it is not often usable possibility

Is This A Good Question/Topic? 0
  • +

Replies To: Name of a variable

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3044
  • View blog
  • Posts: 4,556
  • Joined: 08-June 10

Re: Name of a variable

Posted 13 March 2012 - 12:35 PM

You'd be much better of using an array or an object.
var indexes = [5, 7];
someFunction(index[0]);

// Or
var indexes = {
    1 : 5,
    2 : 7
}
someFunction(indexes[1]);



However, if you absolutely must use an actual variable name, you could, in some situations (like, if it's all happening in the global scope) access the variable as a property of this. You may have to resort to using eval() though, which is why I strongly recommend using an array or object.
Was This Post Helpful? 1
  • +
  • -

#3 Asperant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 11

Re: Name of a variable

Posted 13 March 2012 - 01:06 PM

Atli, Thank You! The solution is eval()
Was This Post Helpful? 0
  • +
  • -

#4 BobRodes  Icon User is offline

  • Your Friendly Local Curmudgeon
  • member icon

Reputation: 547
  • View blog
  • Posts: 2,904
  • Joined: 19-May 09

Re: Name of a variable

Posted 13 March 2012 - 02:42 PM

No it isn't. An array is. Eval is bad. :)
Was This Post Helpful? 0
  • +
  • -

#5 Asperant  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 05-November 11

Re: Name of a variable

Posted 14 March 2012 - 02:13 PM

View PostBobRodes, on 13 March 2012 - 02:42 PM, said:

No it isn't. An array is. Eval is bad. :)


I think - not in this case.
index1
index2
indexABC
indexLALALa, etc.

If to use numbers - ok, but if to use letters ?
Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3044
  • View blog
  • Posts: 4,556
  • Joined: 08-June 10

Re: Name of a variable

Posted 14 March 2012 - 02:46 PM

Then you would move onto my second suggestion: objects. You can use either numbers or strings as property names, making it sort of like an associative array.

If you want a more "array"-like syntax for it than I used in my last example, you can also do this:
var indexes = {};

indexes[1] = 5;
indexes[2] = 9;
indexes["ABC"] = 15;

console.log(indexes[1]); // 5
console.log(indexes["ABC"]); // 15




Whatever you choose to do, just keep in mind that it's rarely a good idea to use eval. It's both insecure and relatively slow. If you can avoid using it (which appears to be the case here) then you should.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1