views:

49

answers:

2

Possible Duplicate:
Determine original name of variable after its passed to a function.

I would like to know if its possible to get the actual name of a variable.

For example:

var foo = 'bar';
function getName(myvar) {  
  //some code
  return "foo"  
};  

So for getName(foo) will return "foo"

Is that possible ?

Thanks.

+2  A: 

I don't think it is possible. When you call a function you pass an object, not a variable. The function doesn't care where the object came from.

You can go the other way though if you call your function as follows:

getName('foo') 

Or pass both the value and the name:

getName(foo, 'foo') 
Mark Byers
Just a comment, why would I pass a harcoded string value 'foo' when I know I am going to get it as return value?
Sachin Shanbhag
@Sachin Shanbhag: How exactly will you get it as a return value if you don't pass it in as a parameter? You're assuming that what the OP is asking is possible. Perhaps it is... but I'd *really* like to see some evidence of that please.
Mark Byers
@MarkByers: I totally agree with you. This is one of the possible ways to getting what is required, but from programming perspective, if I am using that return value for further operations, I already know that value before passing it to function and need not wait for the return value. No offence, but I am actually assuming getting variable name is not posisble since it does not help in programming.
Sachin Shanbhag
A: 

Never seen it done, why would you need it anyway? You already know it. It sounds like you should restructure your scripts to remove this neccessity.

Tom Gullen