-1

How can I get the name of a variable?

Let's say I have an empty variable:

var myvariable;

How can I get the variable name so that this:

alert(this_is_what_i_am_looking_for);

will display:

myvariable

EDIT:

Maybe I am overcomplicating things. My real problem is that I have this two variables:

var firstVariable;
var secondVariable;

And I want to create a third variable that literally has this name:

var firstVariablesecondVariable;

EDIT2:

Thank for the answers, too much information, I have to look at them thoroughly.

This is what I have:

<div></div>
<div></div>
<div></div>

var i = 0;
var q = 0;

$('div').each(function() {
    var qi;
    i++;
});​

So I want to create three varaibles: var 00;var 01;var 02; Then I will load more divs with ajax and do:

q++;
i=0;

so when I apply the each function the second time I have three more variables: var 10;var 11;var 12;

Alvaro
  • 9,247
  • 8
  • 49
  • 76
  • 1
    How you expect to know it? what paramters you want to search with? a value? – Dan Barzilay Sep 02 '12 at 18:21
  • what is "this_is_what_i_am_looking_for". you have to give us something to work with that in some way relates to "myvariable" – Overcode Sep 02 '12 at 18:25
  • This doesn't make a lot of sense as it's written. Perhaps you could expand on the example. How can that `alert()` not know the name? – Pointy Sep 02 '12 at 18:25
  • you have to clarify exactly how you plan on knowing to use variable1 and variable2 to make variable1variable2, as opposed to variable4 or variable9 – Overcode Sep 02 '12 at 18:42
  • Could you explain what you're trying to achieve by creating a variable called `firstVariablesecondVariable`? – georgebrock Sep 02 '12 at 18:45
  • check out this tread: http://stackoverflow.com/questions/1009911/javascript-get-argument-value-and-name-of-passed-variable – piatek Sep 02 '12 at 18:45
  • Sorry if I sound confusing. I edited again. Hope it clarifies. – Alvaro Sep 02 '12 at 18:55
  • @Alvaro, after looking in your edits I would say you need a object hash to store the states of the class (or maybe a clousure, depending of the context). – Marcelo De Zen Sep 02 '12 at 19:28

3 Answers3

3

Variables names are identifiers and you cannot "ask" for an identifier what its name is. What you can do is create a new variable with the contents of the identifier, like this:

 var var1 = "variable1";
 var var2 = "variable2";

 // this will create an property/variable "variable1variable2" 
 // on the global namespace.
 window[var1 + var2] = "xyz";

 var content = variable1variable2; // content = xyz;
Marcelo De Zen
  • 9,439
  • 3
  • 37
  • 50
2

Maybe you could use a hashtable to map name to values. Finally the name barFoo with value barFooTest is added in the hashtable.

var o = { bar: 'barTest' };
o.foo = 'fooTest';

var prevKey = '';
for(key in o)
{   
    prevKey = prevKey + '' + key ;
    alert(key + ': ' + o[key]);
}

o[prevKey] = "barFooTest";
Apostolos Emmanouilidis
  • 7,179
  • 1
  • 24
  • 35
0

this is not possible, what you could do is work with a json object:

var myvars = {
   'firstVariable': 1,
   'secondVariable': 2,
   'firstVariablesecondVariable': function() { return this['firstVariable'] + this['secondVariable']; }
};


console.log(myvars['firstVariable'+'secondVariable']());
​

run code

For edit

var elements = [];
var myformat = function(n) { return (String(n).length == 1 ? '0' : '') + n; }
$('div').each(function(i, el) {
    elements.push(myformat(i));
});

console.log(elements);
​
// response ["00", "01", "02"] 

run code

Edit II

but if you want set values to variables, you can try this:

var elements = {}; // <- object
var myformat = function(n) {
    return (String(n).length == 1 ? '0' : '') + n;
}
$('div').each(function(i, el) {
    elements[myformat(i)] = el;
});

console.log(elements);​

Object
00: HTMLDivElement
01: HTMLDivElement
02: HTMLDivElement

run code

andres descalzo
  • 14,887
  • 13
  • 64
  • 115