473,503 Members | 1,692 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

getObjectName v0.1

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>

Jul 25 '05 #1
3 2544
>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

Jul 26 '05 #2
Baconbutty wrote:
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()
{

}


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.

Jul 27 '05 #3
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.

Jul 27 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1966
by: Greg Swindle | last post by:
Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() {...
17
31639
by: warteschlange | last post by:
is there a way to find out the object/function name from inside object/function. function coffee(){ alert(this.someHowGetMyNameFuncOrVar); => should give me 'coffee' } var milk = new...
4
4894
by: Robert Skidmore | last post by:
I made this javascript to help me debug some javascript one time and I have just been adding to it over the years. It is IE specific (sure it would not be to hard to change that). It is not ever...
1
3365
by: vector | last post by:
There's got to be a better way to do this. In this sample code, I've created a class in a namespace, and a class inside of a class. On calling .ToString() for those two classes, I see different...
2
1329
by: Ken Varn | last post by:
Not sure if this is possible or not or if there is a better method for doing this that I am not seeing. Anyhow, is there anyway to get the declared name of an object that is passed to a function? ...
70
27302
by: jojoba | last post by:
Hello! Does anyone know how to find the name of a python data type. Conside a dictionary: Banana = {} Then, how do i ask python for a string representing the name of the above dictionary...
28
7174
by: davinski | last post by:
So thanks to another member here I got my view sorted out :D but now that I've begun to write my stored proc I've ran into another problem! Basically, my stored proc takes about 30 variables and...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7273
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7322
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4667
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1501
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.