I already knew that I could pass in a object:
{a:"this is a"}
where a is name of the variable but I really want to just pass in a variable or the name of a variable (a or "a") and let the function somehow find the name of the variable and its value.
The reason I want to do this somewhat weird thing is that I am writing a debug print routine. If I call it like this:
var x = "some string";
say(x);
or
say("x");
I want it to print something like:
X (string) = some string
I can pretty reliably find the type of a variable passed, and so I know how to print it, but I really want to try to avoid the redunancy of having to call it like this:
say("x", x);
which is what I have to do now.
What seems to almost work is OrbMan's answer. Sure, eval is evil, but I think it's OK here because it's only for a debug utility and won't be in the published code. I've made a little test routine of my own using that solution:
var x = "this is x";
function say(a) {
alert(a + " = " + eval(a));
}
say("x");
and it works but ONLY IF X IS GLOBAL. This doesn't work:
function wrapper() {
var x = "this is x";
say("x");
}
So, this solution is close, but since I use almost no Global variables, this isn't going to work. Darn, it is soooo close. What I think I need is "call by name" instead of "by value" or "by reference". And I need a function that will work whether it is being called from another function or not.
Since I've put in a good many hours on this myself, asking the question here was an act of desperation, I've got to conclude that there really isn't any way to do this. Shucks.
To everyone who responded, thanks for all your help.