views:

85

answers:

3

I know this is a really long shot, but I figure I'd ask: Is there a way to to find the names of the variables passed as parameters in a function call?

Assuming I have:

function test(tmp1, tmp2) {
  // ...
}

var a;
var b;

test(a, b);

I'd like to get an array like so: [a, b]. Strings would also be acceptable: ["a", "b"].

I do not want ["tmp1", "tmp2"], which I know I can get by parsing the string representation of the function.

I'm asking because I'm trying to to improve my caseclass.js library with real extractors (see the link for more information). I understand that only objects are passed by reference, so I'm trying to find a work around to pass the values extracted back to the placeholder variables.

Thanks!

A: 

Maybe you'd like to take a look at the eval() function... but I've never done something like you describe with javascript. I'm not even sure it's possible.

marcgg
I'm trying to avoid eval, though you'r right that it might be the only way possible. If so, I think I'll avoid it – too hacky then!
pr1001
+1  A: 

maybe you could look into the stacktrace caused by an exception or error, but I'm not certain it would work, and it would probably work differently in different browsers.

Marius
A: 

How about doing something like this:

var a = { name: 'a', value: 'foo' };
var b = { name: 'b', value: 'bar' };

test(a, b);

function test(tmp1, tmp2)
{
   var paramArray = [ tmp1.name, tmp1.name ];
}
cxfx
That works but requires the user to specify the variable name, which I had hoped to avoid. I may have to do that...
pr1001