Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Accessing an object name

Reply
Thread Tools

Accessing an object name

 
 
TonyV
Guest
Posts: n/a
 
      11-26-2007
Hey all, for debugging purposes, I'd like to be able to access an
object's instance name from within the object class. For example, I'd
like to be able to do something like this:

------------------------------------------------------------
function FooClass() {
// definition of FooClass members
}

FooClass.prototype.Debug = function() {
// Need help here to display correct information
alert("My name is ...?");
}

var monkeybutter = new FooClass();
monkeybutter.Debug(); // I'd like this to display "My name is
monkeybutter"
------------------------------------------------------------

Is this possible, and if so, how?
 
Reply With Quote
 
 
 
 
Kailash Nadh
Guest
Posts: n/a
 
      11-26-2007
On Nov 26, 5:57 pm, TonyV <(E-Mail Removed)> wrote:
> Hey all, for debugging purposes, I'd like to be able to access an
> object's instance name from within the object class. For example, I'd
> like to be able to do something like this:
>
> ------------------------------------------------------------
> function FooClass() {
> // definition of FooClass members
>
> }
>
> FooClass.prototype.Debug = function() {
> // Need help here to display correct information
> alert("My name is ...?");
>
> }
>
> var monkeybutter = new FooClass();
> monkeybutter.Debug(); // I'd like this to display "My name is
> monkeybutter"
> ------------------------------------------------------------
>
> Is this possible, and if so, how?


arguments.callee construct might be of help.
http://www.devguru.com/Technologies/...arguments.html

Kailash Nadh | http://kailashnadh.name
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      11-26-2007
On Nov 26, 8:57 pm, TonyV <(E-Mail Removed)> wrote:
> Hey all, for debugging purposes, I'd like to be able to access an
> object's instance name from within the object class. For example, I'd
> like to be able to do something like this:
>
> ------------------------------------------------------------
> function FooClass() {
> // definition of FooClass members
>
> }
>
> FooClass.prototype.Debug = function() {
> // Need help here to display correct information
> alert("My name is ...?");
>
> }
>
> var monkeybutter = new FooClass();
> monkeybutter.Debug(); // I'd like this to display "My name is
> monkeybutter"


var monkeybutter = new FooClass('monkeybutter');
function FooClass() {
this._name = arguments[0] || 'anonymous';
// definition of FooClass members
}

FooClass.prototype.Debug = function() {
alert("My name is " + this._name);
}

 
Reply With Quote
 
John G Harris
Guest
Posts: n/a
 
      11-27-2007
On Mon, 26 Nov 2007 at 09:57:29, in comp.lang.javascript, TonyV wrote:
>Hey all, for debugging purposes, I'd like to be able to access an
>object's instance name from within the object class. For example, I'd
>like to be able to do something like this:
>
>------------------------------------------------------------
>function FooClass() {
> // definition of FooClass members
>}
>
>FooClass.prototype.Debug = function() {
> // Need help here to display correct information
> alert("My name is ...?");
>}
>
>var monkeybutter = new FooClass();


var a = monkeybutter;
var b = a;
var c = b;

>monkeybutter.Debug(); // I'd like this to display "My name is
>monkeybutter"


Now what do you want to display : "a", "b", "c", or "monkeybutter"?
(Note : all those variables point to the same object).

What if you also did

d.e.f["fred"]().g.h = c;

Now what do you want to display?

>------------------------------------------------------------
>
>Is this possible, and if so, how?


After a bit of thought you now realise that that was the wrong question.
As usual, you need to start with the right first question :

"What do I *really* want to do?"

From your first paragraph it looks as though you want each FooClass
object to hold its own object identifier. Then VK's solution is your
answer, though call it 'oid', not 'name', as 'name' is too likely to be
needed for real names. Now the oid can be something convenient or
significant, not restricted to the name of a global variable.

John
--
John Harris
 
Reply With Quote
 
TonyV
Guest
Posts: n/a
 
      11-28-2007
On Nov 27, 3:50 pm, John G Harris <(E-Mail Removed)> wrote:
> var a = monkeybutter;
> var b = a;
> var c = b;
>
> >monkeybutter.Debug(); // I'd like this to display "My name is
> >monkeybutter"

>
> Now what do you want to display : "a", "b", "c", or "monkeybutter"?
> (Note : all those variables point to the same object).


Easy: "monkeybutter."

> After a bit of thought you now realise that that was the wrong question.
> As usual, you need to start with the right first question :
>
> "What do I *really* want to do?"


What I want to do is to have a debugging function in my class that
will tell me which instance of said class the particular debugging
information that follows applies to.
 
Reply With Quote
 
John G Harris
Guest
Posts: n/a
 
      11-29-2007
On Wed, 28 Nov 2007 at 12:22:45, in comp.lang.javascript, TonyV wrote:
>On Nov 27, 3:50 pm, John G Harris <(E-Mail Removed)> wrote:
>> var a = monkeybutter;
>> var b = a;
>> var c = b;
>>
>> >monkeybutter.Debug(); // I'd like this to display "My name is
>> >monkeybutter"

>>
>> Now what do you want to display : "a", "b", "c", or "monkeybutter"?
>> (Note : all those variables point to the same object).

>
>Easy: "monkeybutter."
>
>> After a bit of thought you now realise that that was the wrong question.
>> As usual, you need to start with the right first question :
>>
>> "What do I *really* want to do?"

>
>What I want to do is to have a debugging function in my class that
>will tell me which instance of said class the particular debugging
>information that follows applies to.


I'm afraid you're confusing us.

Your first sentence says you want to know which variable you used to
access an object.

Your second sentence says you want to know which object you accessed.

Which do you really want?

John
--
John Harris
 
Reply With Quote
 
John W. Kennedy
Guest
Posts: n/a
 
      11-30-2007
John G Harris wrote:
> On Wed, 28 Nov 2007 at 12:22:45, in comp.lang.javascript, TonyV wrote:
>> On Nov 27, 3:50 pm, John G Harris <(E-Mail Removed)> wrote:
>>> var a = monkeybutter;
>>> var b = a;
>>> var c = b;
>>>
>>>> monkeybutter.Debug(); // I'd like this to display "My name is
>>>> monkeybutter"
>>> Now what do you want to display : "a", "b", "c", or "monkeybutter"?
>>> (Note : all those variables point to the same object).

>> Easy: "monkeybutter."
>>
>>> After a bit of thought you now realise that that was the wrong question.
>>> As usual, you need to start with the right first question :
>>>
>>> "What do I *really* want to do?"

>> What I want to do is to have a debugging function in my class that
>> will tell me which instance of said class the particular debugging
>> information that follows applies to.

>
> I'm afraid you're confusing us.
>
> Your first sentence says you want to know which variable you used to
> access an object.
>
> Your second sentence says you want to know which object you accessed.
>
> Which do you really want?


It's easy enough to understand once you accept that he's asking for
something grossly impossible; he wants an object to magically remember
the name of the first variable it was ever assigned to.
--
John W. Kennedy
"Compact is becoming contract,
Man only earns and pays."
-- Charles Williams. "Bors to Elayne: On the King's Coins"
 
Reply With Quote
 
TonyV
Guest
Posts: n/a
 
      11-30-2007
On Nov 29, 8:58 pm, "John W. Kennedy" <(E-Mail Removed)> wrote:
> It's easy enough to understand once you accept that he's asking for
> something grossly impossible; he wants an object to magically remember
> the name of the first variable it was ever assigned to.
> --
> John W. Kennedy


There's nothing magical about it, and in spite of the condescending
tone of that reply, it's not rocket science what I'm asking. I want
to know if there's a way for an object instance to have access to
information about itself, such as its name, if it has one.

If it can't, then just say so. If it can, then I'd appreciate knowing
how.

> var a = monkeybutter;
> var b = a;
> var c = b;
>
> >monkeybutter.Debug(); // I'd like this to display "My name is
> >monkeybutter"

>
> Now what do you want to display : "a", "b", "c", or "monkeybutter"?
> (Note : all those variables point to the same object).


In a case like this, as I said, I'd love to have "monkeybutter," but
I'd even be pleased for something like an array containing the values
"monkeybutter," "a," "b," and "c" if multiple references to the same
object instance exists.

I never said I wanted to know which object was being accessed. I've
been consistent in what I need the whole time: I need to know which
object *instance* is being accessed to report for debugging purposes.
If there's some kind of problem with an object instance, I'd like for
the instance to be able to report, "Hey, this specific thing has
something wrong with it. (As opposed to all the other things of the
same type that you've declared.)" I guess I'm confused as to why
there's so much confusion, the question seems pretty straight-forward,
even if the answer is not.
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      11-30-2007
TonyV wrote:
> On Nov 29, 8:58 pm, "John W. Kennedy" <(E-Mail Removed)> wrote:
>> It's easy enough to understand once you accept that he's asking for
>> something grossly impossible; he wants an object to magically remember
>> the name of the first variable it was ever assigned to.
>> --
>> John W. Kennedy

>
> There's nothing magical about it, and in spite of the condescending
> tone of that reply, it's not rocket science what I'm asking.


Yes, there is; and yes, you are. The variable stores but an object
reference, one of maybe many, which may have been created in a number
of different ways.


PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      11-30-2007
On Nov 30, 10:02 pm, TonyV <(E-Mail Removed)> wrote:
> I never said I wanted to know which object was being accessed. I've
> been consistent in what I need the whole time: I need to know which
> object *instance* is being accessed to report for debugging purposes.


You cannot do it - not in JavaScript nor in other languages AFAIK but
my knowledge is limited.

> If there's some kind of problem with an object instance, I'd like for
> the instance to be able to report, "Hey, this specific thing has
> something wrong with it.


For that you have to custom mark each new instance as suggested in my
first post so to report it later.

No other way around. I hope it answers your question.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
How To: Allow For Accessing site By Only the Domain Name, Without the Host Name? Joey ASP .Net 0 09-15-2005 03:04 AM
Invalid object name accessing SQL Jerry Nelson ASP .Net 2 10-16-2004 06:20 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments