Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the 'name' of a variable in Javascript [duplicate]

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.

like image 353
shdev Avatar asked Aug 25 '10 10:08

shdev


People also ask

Can you have two variables with the same name JavaScript?

It's not possible, an if statement has no special scope, so you can't have two variables with the same name within the same scope and access both, the latter will overwrite the former, so they should have different names.

What is copy by value in JavaScript?

In Javascript, primitives (strings, numbers, etc) are passed by value, and copied. Objects, including arrays, are passed by reference. In any case, assignment of a new value or object reference to 'a' will not change 'b'. But changing the contents of 'a' will change the contents of 'b'.

What is copy by value and copy by reference in JavaScript?

When you copy an object b = a both variables will point to the same address. This behavior is called copy by reference value. Strictly speaking in Ruby and JavaScript everything is copied by value. When it comes to objects though, the values happen to be the memory addresses of those objects.


1 Answers

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') 
like image 77
Mark Byers Avatar answered Oct 22 '22 07:10

Mark Byers