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.
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.
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'.
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.
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')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With