sorry for the top post... but the subject has been made thoroughly familiar.
No need for eval...
and, if I understand what you're trying to get at...
// if title is set in html:
<title>test of compoundAlert</title>
// otherwise
document.title = "test of compoundAlert";
<script...
// globals are children of window
// except those specifically attached to document [window.document]
// some stuff to test:
var c = 123;
var str = "this is a string";
var f = function () {
var local = "test";
return local + " this";
}
//I'm gonna go out on a limb here -- just for grins:
function
myObj() {
this.prop = "property";
this.meth = function(arg)
{
return arg;
}
}
var o = new myObj();
// gonna need this to grab the object constructor's name:
function
getConstructor(o)
{
var s = new String(o.constructor);
// grab everything after 'function and before '('
var part = s.match(/function\s+([^\(]+)/);
var name = part ? part[1].replace(/ /g,"") : "function"; // see ** note
// the replace strips any unwanted spaces due to
// "coding style" of: function myObj ( arglist )...
return name;
}
/* ** note: if object is declared:
myObj = function() {...}
"myObj" will not be returned -- "function" will --
and the variable name will be used in compoundAlert
*/
function
compoundAlert()
{
var format = "";
for(var i = 0; i < arguments.length; i++)
{
var argname = arguments[i];
// test for document element otherwise use window
var parent = argname.indexOf("document.") != -1 ? document : window;
if(parent == document)
argname = argname.replace(/document\./,"");
var type = typeof(parent[argname]); // use associative indexing
var value = parent[argname];
format += "argument name: " + argname + "\n" +
"argument type: " + type + "\n" +
"value of argument:\n" + value + "\n\n";
// just for grins... a little lagniappe... expand objects...
if(type == "object")
{
var tmp = parent[argname];
var cnst = getConstructor(tmp); // const is reserved keyword
if(cnst == "function") // failsafe
cnst = argname;
format += "Object: " + cnst + "\n";
for(var a in tmp)
{
format += a + ": " + tmp[a] + "\n";
}
format += "\n";
}
}
alert(format);
}
compoundAlert('c','str','f','document.title', 'o');
// all must be passed as strings for varname = varvalue formatting
should get you started... Things like "Math" methods, etc... are not
covered here -- an exercise for you to work out (not that there's really
anything useful to get -- Math methods are native code and will display
'[native code]')
if you have any trouble running this, let me know -- i developed it on a
pc and hand-copied it over to a mac, made several revisions and patches
before sending...there might be typos.
Fox
****************
optimistx wrote:
How to write a function f(x) or f('x') so that it produces the same output
as a call to alert-function:
var x='abc';
alert('x='+x);
In testing one needs to write similar alert() calls often. By having a
function f(), which would add the variable name in front of the variable
value one would save a lot of typing during one's lifetime.
I think there is so simple solution for this that I probable will feel very
embarrassed after getting the answer. So I blush in advance :).