tags:

views:

67

answers:

2

Hi,

How to alert variable name, not a value of variable?

var color = 'red';

alert(color); // Will alert 'red'
alert(/* magic */); // Will alert 'color'
+2  A: 

In the Firebug console:

>>> a=[]
[]
>>> a
[]
>>> b=a
[]
>>> a.push(3)
1
>>> b
[3]
>>> a
[3]

So, which variable name would you like that array to return? a? b? Something completely different?

Ignacio Vazquez-Abrams
+2  A: 

It's not possible in JavaScript, because arguments in this language are passed by value or by reference, not by name, so when variable is passed to function, its name is lost.

el.pescado
Unless it's a function declaration, in which case we can `toString` it ;)
J-P
Not always: var foo = function () {alert ("foo");}; alert (foo.toString ()); won't give you function name.
el.pescado
So how work all templating engines written in Javascript?
Randy Gurment
@el.pescado Well, yeh, because that isn't a "function declaration"...
J-P