Javascript Find argument passed to a function

Viewed 669

I am needing to find the argument passed to a function from the function.

Let us suppose I have a function called foo:

function foo() {
  var a = 3;
  var b = "hello";
  var c = [0,4];
  bar(a - b / c);
  bar(c * a + b);
}

function bar(arg) { alert(arg) }

As it is now, of course, bar will always alert NaN.

Inside of the function bar, I want to obtain the argument in the form it was originally passed. Furthermore, I want to be able to access the values of a, b, and c from the bar function. In other words, I would like something of this nature:

bar(a - b / c);    

function bar() {
 //some magic code here
 alert(originalArg); //will alert "a - b / c"
 alert(vars.a + " " + vars.b + " " + vars.c); //will alert "3 hello 0,4"
} 

You may not think this is possible, but I know you can do weird things with Javascript. For example, you can display the code of the calling function like this:

function bar() {
  alert(bar.caller);
}

I am willing to bet a few dollars that with some sort of finagling you can get a the original form of the argument and the values of the variables in the argument.

I can easily see how you could do it if you were allowed to call bar in a form like this:

bar(function(){return a - b / c}, {a: a, b: b, c: c});

But that is too convoluted and therefore unacceptable. The way bar is called may not be changed.

6 Answers
Related