getObjectName v0.1

V

VK

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>
 
B

Baconbutty

A need of such script is more than questionnable

It is useful if you want to serialise your data.

Assuming I create some constructor object for a particular kind of
data:-

function myData()
{

}

If I want to serialise this properly, I also need to know the name of
the constructor (myData), so that when I unserialise I know which
constructor to call.

A serialised form could be

[
[
"myData",
{
"property":"value"
"property":"value"
"property":"value"
}
],
[
"myData",
{
"property":"value"
"property":"value"
"property":"value"
}
]
]

When unserialising I can then use:-

var oInstance=new window[sConstructor]();
oInstance.property=value;
etc
 
C

Christopher J. Hahn

Baconbutty said:
It is useful if you want to serialise your data.

Assuming I create some constructor object for a particular kind of
data:-

function myData()
{

}

Consider adding:

myData.prototype = {
property1: value1,
property2: value2,
constructor: myData
}
If I want to serialise this properly, I also need to know the name of
the constructor (myData), so that when I unserialise I know which
constructor to call.

A serialised form could be

[
{
property1: value1,
property2: value2,
constructor: myData
}
]

[snip]
When unserialising I can then use:-

var oInstance=new window[sConstructor]();
oInstance.property=value;
etc

or:
// assuming you've parsed it into an actual var oData
var oInstance = new oData[0].constructor( oData[0] );


Write your constructors to accept objects that are based on what
they're constructing, using the properties of the passed object to
override defaults in the prototype/constructor.

Of course, doing it this way means you're also serializing actual code,
the benefits, drawbacks, and adviseability of which will vary.

Just something to think about.
 
B

Baconbutty

Thank you Christohper.

They are some interesting points to consider.

I will experiment with them.

I have been looking at different ways of serialising data, simply
because my only coding skills are in Javascript, so as well as for web
pages, I use DHTML to write on-the-fly mini browser-applications for
use in my work (e.g. I have written a small personal time recording
application) which require data to be serialised.

Thanks again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top