This script allows you to get the object name literal as string.
It works as long as caller property is supported and as long as a call
chain is presented (thus one could backtrace to the holder of the
literal).
A need of such script is more than questionnable. I guess these
literals just got me of being too dazzling: you see them, you can touch
them, but you cannot get their damn name despite it's just in front of
you. :-)
<html>
<head>
<title>Object name</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script>
var foo = new Object();
var bar = foo;
function test(obj) {
alert(getObjectName(obj));
}
function getObjectName(obj) {
var thisCaller = getObjectName.caller;
// Go through the call chain to find
// the true name holder:
while ((thisCaller)&&(thisCaller.caller)) {
thisCaller = thisCaller.caller;
}
if (thisCaller) {
var re = /(\(\s*)(.+)(\s*\))/;
var f = thisCaller.toString();
re.exec(f);
return RegExp.$2;
}
else {
// caller property is not supported
// or the call chain is broken
return 'undefined';
}
}
</script>
</head>
<body bgcolor="#FFFFFF" onload="test(foo)">
</body>
</html>