Dr said:
It cannot be done with f(x), as the name is not then passed. But
function f(x) { alert(x+'='+eval(x)) }
will do it.
A general expression can be passed; f("3+5"), f("Math.sqrt(+X)").
Since that will not show the difference between x="" & x=" ", you may
want a different version for strings,
function s(x) { alert(x+'="'+eval(x)+'"') }
Or, something in the nature of the following framework:
ALERT = eval; // Slip sheep's clothing on nasty wolf!
function fmt(arg) { // Create eval-ready alert string
var varList = arg.split(",");
for (var k= 0, str = ""; k < varList.length; k++) {
str += "+'\\n"+varList[k]+ ": '"
+"+ (( typeof "+varList[k]+" == 'object') ? Dumper("+varList[k]
+") : "+varList[k]+"+ ' type: '+ typeof "+varList[k]+" )";
}
return "alert(" +str.substr(1)+")";
}
function test(a,b) {
var c = "local variable";
var d= document.title;
var e = { propName : "property value" };
ALERT(fmt("a,b,c,d,e"));
}
test("arg1",2);
which extends the display to include objects.
___________
Notes:
- Creating the ALERT reference to the built-in "eval" function
appears to work in IE 6 and Opera 7.11. It fails in Netscape/Mozilla
in that local execution context is not maintained (however, the above
works if eval is used without substitution).
- Dumper is an object formatting function from the following
reference:
<url:
http://groups.google.ca/groups?q="m...g=d&[email protected]&rnum=7>
- It appears that the line
DumperTagProperties["OPTION"] = ['text','value','defaultSelected'];
needs to be included (at least currently) if the object to be
formatted references an element from the DOM (as per the last line of
the example given in the reference). Without it, the library code
loops when executing Dumper(x);.
___________
There is a way, I suspect, of doing it without eval.
I confess to being doubtful. It would seem to me that eval is the only
mechanism available that is going to allow insertion/execution of code
that takes on the required context.
../rh