473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Name of class instance

Hi,

I'm wondering if it's possible for an object to figure it's name.
for example:

function baseClass() {
// setup properties of "baseClass"
}
baseClass.prototype = {
aMethod: function() {
// do something using the name of the object
// that's an instance of this (or derived) class
// e.g.
// window.alert('my name is: "' + DUNNO + '"');
};
};

function childOne() {
// setup properties of "childOne"
}
childOne.prototype = new baseclass();

function childTwo() {
// setup properties of "childTwo"
}
childTwo.prototype = new baseclass();

var o1 = new childOne(), o2 = new childTwo();

Now when calling "o1.aMethod()" the "DUNNO" should be "o1" and the
same with "o2" respectively.

Is that possibly without having to force a "name" argument down the
inheritance tree?

--
Matthias
/"\
\ / ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL
X - AGAINST M$ ATTACHMENTS
/ \
Jun 27 '08 #1
8 1647
On May 6, 8:31 am, Matthias Watermann wrote:
I'm wondering if it's possible for an object to figure
it's name.
Objects don't have names, unless you give them names in which case it
is as easy to figure those names out as you programmed it to be when
you gave them their names.

<snip>
var o1 = new childOne(), o2 = new childTwo();

Now when calling "o1.aMethod()" the "DUNNO" should be "o1"
and the same with "o2" respectively.
And after:-

var o = o1;

o.aMethod();

- what is supposed be appear?
Jun 27 '08 #2
VK
On May 6, 11:31 am, Matthias Watermann <li...@mwat.dewrote:
Hi,

I'm wondering if it's possible for an object to figure it's name.
for example:

function baseClass() {
// setup properties of "baseClass"}

baseClass.prototype = {
aMethod: function() {
// do something using the name of the object
// that's an instance of this (or derived) class
// e.g.
// window.alert('my name is: "' + DUNNO + '"');
};

};

function childOne() {
// setup properties of "childOne"}

childOne.prototype = new baseclass();

function childTwo() {
// setup properties of "childTwo"}

childTwo.prototype = new baseclass();

var o1 = new childOne(), o2 = new childTwo();

Now when calling "o1.aMethod()" the "DUNNO" should be "o1" and the
same with "o2" respectively.
and with
var o1 = new childOne();
// ...
var foo = o1;
foo.aMethod();

what DUNNO should be, "o1" or "foo"?

From the other side if it is guaranteed that for each instance only
one reference will exist, then just hardcode it:

var o1 = new childOne("o1");

From my rather long experience which you are free to disregard of
course, the "necessity" to know the current instance reference
identifier is a strong indication that the current programming pattern
is targeted straight to hell where it will sooner or later ;-)
So it may be a good idea to stop right now so taking some more secure
direction. With the block schema explained more practical suggestions
could be given.
Jun 27 '08 #3
On Tue, 06 May 2008 03:09:03 -0700, VK wrote:
[...]
>var o1 = new childOne(), o2 = new childTwo();

Now when calling "o1.aMethod()" the "DUNNO" should be "o1" and the
same with "o2" respectively.

and with
var o1 = new childOne();
// ...
var foo = o1;
foo.aMethod();

what DUNNO should be, "o1" or "foo"?
Theoretically "foo". But in practice I don't care as I do not use such
a construct. (No offence meant.)
From the other side if it is guaranteed that for each instance only
one reference will exist, then just hardcode it:
Well, as long as I had only one instance I did it that way. But now I
need multiple instances of (different) derived classes. So the
hardcoded singleton approach doesn't work for me.

Now, that I think of it, the whole inheritance thing is just a little
complication. The main question, however, is whether JavaScript
provides a way to let an object figure out its name. I understand
that's not the case, right?
[...]
From my rather long experience which you are free to disregard of
course, the "necessity" to know the current instance reference
identifier is a strong indication that the current programming pattern
is targeted straight to hell where it will sooner or later ;-)
Well, I can't reject that, of course. In my case the object's name
is needed for sending it (along with other data) back to the web-server
which returns a piece of JavaScript code invocing an instance method
(the name and API of which is defined by contract). And since there may
be several concurrent requests (by different objects) I've to make sure
somehow that each response reaches the respective requester.

I've thought about a static class method of the base class to be
called instead. But apart from the fact that such a class method
would have to keep track of all instanciations (and deletions) it
would still need some way to identify the object that's supposed
to handle the received data.

In case you can think of another approach I'd appreciate it. Any
pointers are welcome.
--
Matthias
/"\
\ / ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL
X - AGAINST M$ ATTACHMENTS
/ \
Jun 27 '08 #4
VK
On May 6, 3:25 pm, Matthias Watermann <li...@mwat.dewrote:
The main question, however, is whether JavaScript
provides a way to let an object figure out its name. I understand
that's not the case, right?
Right - because there is not such thing as "instance name". In this
aspect Javascript is no any different from any other programming
language like say C++ or Java. There is an instance object, and a
reference to this instance can be assigned to different identifiers or
you even may keep it anonymous, say
(new childOne).aMethod();
- not to say this approach is used too often, just to help to separate
in your mind a reference identifier and the object itself.
Of course each instance has its own objectID (DispID) to distinguish
several instances of the same class, but it is an internal system
property which is not accessible from the language itself. Again, it
is not any different from other OO languages.
>
[...]
From my rather long experience which you are free to disregard of
course, the "necessity" to know the current instance reference
identifier is a strong indication that the current programming pattern
is targeted straight to hell where it will sooner or later ;-)

Well, I can't reject that, of course. In my case the object's name
is needed for sending it (along with other data) back to the web-server
which returns a piece of JavaScript code invocing an instance method
(the name and API of which is defined by contract). And since there may
be several concurrent requests (by different objects) I've to make sure
somehow that each response reaches the respective requester.
A month later I would suggest to use WebService mechanics for that,
but now I see confirmed that Firefox 3.x will not have it as a default
feature for whatever reason:
http://developer.mozilla.org/en/docs..._WSDL_Proxying

So just stick then with manually setting initial instance identifier
on object instantiation. It is very bad and all hell doors may get
loose if more than one reference to the same object, but if you are on
a time limit...

For a long right run it is not instance business to handle RMI (Remote
Method Invocation), they have to do their job right first. For RMI
there should be a separate dispatcher. This way an instance sends
request to the dispatcher with reference to itself as request
consumer. The dispatcher then handles the request queue and returns
results to consumers by stored references.
This way you don't care what identifier(s) is(are) currently used to
hold a reference to the object which is the proper way to do things.
Jun 27 '08 #5
On Tue, 06 May 2008 05:00:58 -0700, VK wrote:
[...]
For a long right run it is not instance business to handle RMI (Remote
Method Invocation), they have to do their job right first. For RMI
there should be a separate dispatcher. This way an instance sends
request to the dispatcher with reference to itself as request
consumer. The dispatcher then handles the request queue and returns
results to consumers by stored references.
This way you don't care what identifier(s) is(are) currently used to
hold a reference to the object which is the proper way to do things.
Ah, thank you very much! I definitely like such an approach. Quite
possibly that would allow for omitting the whole inheritance stuff and
use a design-by-contract model instead. Hmm, the more I think of it
the more charming it becomes ...
--
Matthias
/"\
\ / ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL
X - AGAINST M$ ATTACHMENTS
/ \
Jun 27 '08 #6
Matthias Watermann wrote:
On Tue, 06 May 2008 05:00:58 -0700, VK wrote:
>[...]
For a long right run it is not instance business to handle RMI (Remote
Method Invocation), they have to do their job right first. For RMI
there should be a separate dispatcher. This way an instance sends
request to the dispatcher with reference to itself as request
consumer. The dispatcher then handles the request queue and returns
results to consumers by stored references.
This way you don't care what identifier(s) is(are) currently used to
hold a reference to the object which is the proper way to do things.

Ah, thank you very much! I definitely like such an approach. Quite
possibly that would allow for omitting the whole inheritance stuff and
use a design-by-contract model instead. Hmm, the more I think of it
the more charming it becomes ...

Try this one line:

<input type="button" onclick="alert('My name is ' + this.id)" id="foo"
value="Click Me">

and see what happens for you. Yes, each tag needs to be given a name
for this to work, but if you want a generic javascript with a generic
calling method, the use of "this" will work and give you the objects id.
Jun 27 '08 #7
On Tue, 06 May 2008 10:48:00 -0400, sheldonlg wrote:
>>[...]
For a long right run it is not instance business to handle RMI (Remote
Method Invocation), they have to do their job right first. For RMI
there should be a separate dispatcher. This way an instance sends
request to the dispatcher with reference to itself as request
consumer. The dispatcher then handles the request queue and returns
results to consumers by stored references.
This way you don't care what identifier(s) is(are) currently used to
hold a reference to the object which is the proper way to do things.

Ah, thank you very much! I definitely like such an approach. Quite
possibly that would allow for omitting the whole inheritance stuff and
use a design-by-contract model instead. Hmm, the more I think of it
the more charming it becomes ...

Try this one line:

<input type="button" onclick="alert('My name is ' + this.id)" id="foo"
value="Click Me">

and see what happens for you. Yes, each tag needs to be given a name
for this to work, but if you want a generic javascript with a generic
calling method, the use of "this" will work and give you the objects id.
My markup doesn't contain any "onXXX" attributes since I prefer to clearly
separate markup (HTML), presentation (CSS) and behaviour (JavaScript). But
I see your point. However, my objects are not tied to a certain tag (i.e.
its JavaScript/DOM incarnation); sometimes they may handle several page
elements - like, say all anchors - sometimes they become active somewhere
in the event chain controlled by the respective method arguments. So it's
not a tag identifier I was looking for (that's easy enough as you pointed
out) but the name of a JavaScript object which I intended to send to the
remote server. Thanks for your thoughts anyway.
--
Matthias
/"\
\ / ASCII RIBBON CAMPAIGN - AGAINST HTML MAIL
X - AGAINST M$ ATTACHMENTS
/ \
Jun 27 '08 #8
Matthias Watermann wrote:
On Tue, 06 May 2008 03:09:03 -0700, VK wrote:
>[...]
>>var o1 = new childOne(), o2 = new childTwo();

Now when calling "o1.aMethod()" the "DUNNO" should be "o1" and the
same with "o2" respectively.
and with
var o1 = new childOne();
// ...
var foo = o1;
foo.aMethod();

what DUNNO should be, "o1" or "foo"?

Theoretically "foo". But in practice I don't care as I do not use such
a construct. (No offence meant.)
You miss the point. Objects just don't have names, they have identity.
Properties have names. Object references can be values of properties, and
there can be any number of different properties referencing the same object.
To keep track of all of them would require you to implement a record object
that is used every time a property of an object is added, modified or
deleted. And that is not only impossible as there are built-in objects and
host-defined properties, but the result retrieved then would be far from
being usable for debugging.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Jun 27 '08 #9

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

Similar topics

5
by: sinasalek | last post by:
class TTest { function instance_name() { echo "instance name : $instance_name"; } } $test=new TTest(); $text->instance_name();
6
by: Andre Meyer | last post by:
Hi all I have been searching everywhere for this, but have not found a solution, yet. What I need is to create an object that is an instance of a class (NOT a class instance!) of which I only...
33
by: Jacek Generowicz | last post by:
I would like to write a metaclass which would allow me to overload names in the definition of its instances, like this class Foo(object): __metaclass__ = OverloadingClass att = 1 att = 3
7
by: max(01)* | last post by:
hi. is there a way to define a class method which prints the instance name? e.g.: >>> class class_1: .... def myName(self): .... ????what should i do here???? ....
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
2
by: Bill D | last post by:
In a simple Windows forms application, the main form is called Form1. Within this form's class, I refer to the the running instance of this main class as "this" as in this.Text = "NEW TEXT" I...
12
by: guy lateur | last post by:
Hi all, Suppose you have this class: class foo: def bar(): Suppose you also have the strings "foo" and "bar". How can you obtain the function foo.bar()?
4
by: =?Utf-8?B?SmFzb24gUmV5bm9sZHM=?= | last post by:
(using .net 2.0) Say you have a class structure like this: class Address .... end class class Person FirstName
3
by: Jon | last post by:
My main form opens up another form, and from this other form, I'd like to access things in the main form. The problem is that although I know the name of the class of the main form (FormMain) I...
23
by: Hugh Oxford | last post by:
How do I get an object's name? EG. $obj_FOO = new Bar; echo $obj_FOO->getName(); 'obj_FOO'
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.