1

I want to create a new variable in javascript but it's name should made of a stale part and a variable one like this:

tab_counter = 1;
var editor + tab_counter = blabla

well i want the new variable name to be in this case editor1, is this possible?

Pointy
  • 405,095
  • 59
  • 585
  • 614
Matteo Pagliazzi
  • 5,140
  • 12
  • 49
  • 83

4 Answers4

6

You cannot create a stand-alone variable name that way (except as a global) (edit or except with eval()), but you can create an object property:

var tab_counter = 1;
var someObject = {};
someObject['editor' + tab_counter] = "bla bla";

You can create globals as "window" properties like that, but you probably shouldn't because global variables make kittens cry.

(Now, if you're really just indexing by an increasing counter, you might consider just using an array.)

edit also see @Birey's somewhat disturbing but completely correct observation that you can use "eval()" to do what you want.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • All you kind people up-voting me are ignoring the fact that the answer isn't exactly 100% correct as originally written: the "eval()" built-in can do what the OP wants, if he can stomach it! – Pointy Nov 04 '11 at 15:31
  • I upvoted for your kindness towards kittens. I think the answer sucks :D – El Ronnoco Nov 04 '11 at 15:35
2

It is possible

var tab_counter=1;
eval("var editor"+tab_counter+"='blah'")
alert(editor1);
eval("var editor"+tab_counter+1+";")
editor2='blahblah';
alert(editor2);

http://jsfiddle.net/5ZLYe/

Birey
  • 1,764
  • 15
  • 20
  • 1
    You are the creator of your code, you should know your variables. Explain your situation briefly, what exactly you are trying to achieve. – Birey Nov 04 '11 at 15:39
  • I have a variable number of tabs in my layout: each time i create a new tab it do tab_counter++ now i want to create a a new var for each tab editor+tab_counter and we're ok. Then the user is in tab number 16 and there is a variable called tab_current = 16; now i want to use editor16 using tab_current, how can i do this? – Matteo Pagliazzi Nov 04 '11 at 15:39
  • @Matteo Have an array called `tabs` and when you create a new tab store a reference to it in `tabs`. eg `tabs[tab_counter] = newTab`. – El Ronnoco Nov 04 '11 at 15:45
  • When you can create a variable using eval() , you can use it too. see last line in : http://jsfiddle.net/5ZLYe/1/ – Birey Nov 04 '11 at 15:48
  • 1
    Array would be much better way to do it though. – Birey Nov 04 '11 at 15:48
  • +1 For fulfilling the OP's odd requirement but saying Array would be better :) – El Ronnoco Nov 04 '11 at 16:13
1

You can do the eval method used by Birey or you can create a custom property of an object such as...

obj[editor + tab_counter] = blabla;

But it sounds like you're going about doing whatever you're doing in a particularly horrible way. If you just want to store multiple items which you can index into use an array...

var array = [];
array[0]  = blabla;
array[1]  = blabla2;

alert(array[0]); //shows value of blabla
alert(array[1]); //shows value of blabla2
El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
0

It seems like you may want to consider using a Dictionary for something like this. This link which references this link describes your options there.

Community
  • 1
  • 1
sky-dev
  • 6,190
  • 2
  • 33
  • 32