Accessing an object name

T

TonyV

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"
 
K

Kailash Nadh

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"

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

Kailash Nadh | http://kailashnadh.name
 
V

VK

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);
}
 
J

John G Harris

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?

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
 
T

TonyV

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


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.
 
J

John G Harris

Easy: "monkeybutter."


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
 
J

John W. Kennedy

John said:
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.
 
T

TonyV

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.

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;


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.
 
T

Thomas 'PointedEars' Lahn

TonyV said:
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
 
V

VK

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.
 
J

John G Harris

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.

You're guessing. So am I. The trouble is that he knows what answer he's
expecting but doesn't know, or can't describe, what question it's
answering. Until we know the question we don't know which of six
different solutions to suggest.

John
 
J

John W. Kennedy

John said:
You're guessing. So am I. The trouble is that he knows what answer he's
expecting but doesn't know, or can't describe, what question it's
answering. Until we know the question we don't know which of six
different solutions to suggest.

No, actually, I'm really not guessing. He is saying very plainly and
unambiguously what it is that he wants; we're just having trouble
understanding it because anyone with the slightest knowledge of
programming -- in any language -- knows that what he's asking for is
complete nonsense, and so our instincts make us want to believe that
he's asking for something else, something sensible, instead. Humans are
patter-matchers. But if you read his original message, it's very clear
that he has gotten the notion into his head that the statement

variablename = new objecttype;

somehow forges a permanent and bilateral tie between the newly created
object and the assigned-to variable -- that the new object is somehow
"really" {variablename}, irrespective of whatever other variables it may
subsequently be assigned to. I did not use the word "magic" arbitrarily
here; this is essentially an analog of the magical Law of Contagion:
"Objects once in contact continue to interact at a distance."
 
V

VK

No, actually, I'm really not guessing. He is saying very plainly and
unambiguously what it is that he wants; we're just having trouble
understanding it because anyone with the slightest knowledge of
programming -- in any language -- knows that what he's asking for is
complete nonsense, and so our instincts make us want to believe that
he's asking for something else, something sensible, instead. Humans are
patter-matchers. But if you read his original message, it's very clear
that he has gotten the notion into his head that the statement

variablename = new objecttype;

somehow forges a permanent and bilateral tie between the newly created
object and the assigned-to variable -- that the new object is somehow
"really" {variablename}, irrespective of whatever other variables it may
subsequently be assigned to.

OP also says: "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.)".

So being spelled in the programming terms, OP wants to know:
1) if there is a way to access instance OID (Object identifier) in
JavaScript.
2) if it is so then if there is a way to match the internal OID to the
higher level execution flow thus to find out what instance with such
OID matches to one created in the program statements.

The answer is "No" in both cases. OP is right in his assumption that
no matter how many reference values to an instance are existing
runtime - it is always the same heap with the same internal OID. The
deal is that it is not accessible from the top level and even if it
was then it is not correlated with top level identifiers.
 
T

Thomas 'PointedEars' Lahn

VK said:
[...] But if you read his original message, it's very clear
that he has gotten the notion into his head that the statement

variablename = new objecttype;

somehow forges a permanent and bilateral tie between the newly created
object and the assigned-to variable -- that the new object is somehow
"really" {variablename}, irrespective of whatever other variables it may
subsequently be assigned to.

OP also says: "I need to know which object *instance* is being accessed
to report for debugging purposes.

Objects have identity, not names (unless you give the object a property that
is its name, as suggested). References can have names, but there can be
more than one reference to the same object. Which one of those names,
provided there is one, would you like the method to return?


PointedEars
 
T

TonyV

No, actually, I'm really not guessing. He is saying very plainly and
unambiguously what it is that he wants; we're just having trouble
understanding it because anyone with the slightest knowledge of
programming -- in any language -- knows that what he's asking for is
complete nonsense...

This is the last reply I'm going to post because it seems that it
can't be done in Javascript, but I just had to reply to this that just
because you apparently don't know a lot about programming is no reason
to project your inadequacy on others.

As most people who *actually* have knowledge about programming know,
almost every language maintains something called a "symbol table" that
associates names of things with what and where they are. When you say
something like:
var foo = 12;

the symbol table is what associates the memory location that holds the
value of 12 with the name "foo." Later, if you say:
foo += 4;

Javascript uses the symbol table to know what exactly is supposed to
be incremented by four. I know, this is all pretty sophisticated and
might seem to work like "magic" as far as you can tell. But I assure
you, it's *not* magic, it's *not* nonsense, and it usually works
pretty well behind the scenes with no need to worry much about it.

However, there might be an occasion--for example, if one were working
on a debugging function--when it would be handy to have limited access
to the symbol table, preferably through some mechanism built into the
language itself instead of through a separate debugging program or
creating fields to manually store instance names. That way, if some
function fails on one of a thousand or so object instances within a
program, the debugging function could say something like, "Hey, the
foo object is the one that's hosed up" at runtime, making
troubleshooting the problem much easier.

Maybe you've never worked with a debugger that has access to programs'
symbol tables and that can show you this information. I have, but not
in Javascript, though I'm certain they exist. Maybe you've never
heard of a stack trace, or think that it really does work by magic
when it looks like it's pulling the names of calling functions out of
thin air. I assure you, though, that such concepts aren't nonsense,
they're extremely practical and used quite frequently by *real*
programmers.

All I needed to know is if there's a way to get this information at
runtime, which from what I've read here, there's not. That's all,
question answered, no need to be condescending.

I apologize if my attempt at simplification was confusing. I figured
that if Javascript did have some mechanism to access the name(s) of
objects within those objects, it would probably be something
relatively obvious to someone who's worked with the language for a
while, and talking about symbol tables and stack traces would have
been overkill and would have been more confusing, not less.

I sincerely hope that next time you don't know something, you actually
try learning a few things when you're out of your league instead of
treating people who are likely smarter and more experienced than you
like they're stupid.

To the others who have posted in this thread, thanks for the input.
I'll probably just go with VK's original suggestion of manually
telling new objects what name I've given them.
 
J

John W. Kennedy

This is the last reply I'm going to post because it seems that it
can't be done in Javascript, but I just had to reply to this that just
because you apparently don't know a lot about programming is no reason
to project your inadequacy on others.

I've been programming, in dozens of languages, for over forty years.
As most people who *actually* have knowledge about programming know,
almost every language maintains something called a "symbol table" that
associates names of things with what and where they are. When you say
something like:
var foo = 12;

"Languages" do no such thing. Interpreters and compilers do, but
compilers, except in special debug modes, normally discard the
information long before execution begins.
the symbol table is what associates the memory location that holds the
value of 12 with the name "foo." Later, if you say:
foo += 4;
Javascript uses the symbol table to know what exactly is supposed to
be incremented by four. I know, this is all pretty sophisticated and
might seem to work like "magic" as far as you can tell. But I assure
you, it's *not* magic, it's *not* nonsense, and it usually works
pretty well behind the scenes with no need to worry much about it.

That is not an analogy to what you asked for, which was (to continue
with this example) that the number 12 should somehow remember that its
name is "foo" -- even after it has been assigned to other variables. You
explained that that was what you wanted at great length in your original
post.

And that /is/ nonsense, and that's why most of the people here couldn't
even figure out what you wanted; it was too far from reality.
 
E

Evertjan.

TonyV wrote on 03 dec 2007 in comp.lang.javascript:
However, there might be an occasion--for example, if one were working
on a debugging function--when it would be handy to have limited access
to the symbol table, preferably through some mechanism built into the
language itself instead of through a separate debugging program or
creating fields to manually store instance names.

This is all very well for a language that has one implementation engine,
and the so called [by you] symbol table has a reachable position in the
memory stack or memory address space.

However Javascript is just a specification, and the actual execution is
on a wide variatry of implementations, so anything you want to do outside
of the specs, even if you succede with your own engine will probably fail
on others. Cross browser friendly that is called in clientside js.

Even a language like Forth, that can meta-compile it's own kernel engine,
and has access to the symbol table in specs, has in the years become a
language with so many flavours, that a general method for such things
should be tested on each flavour.

Javascript, having only high level specs, cannot help you in this. And
Javascript in sensu strictior is just specs.
That way, if some
function fails on one of a thousand or so object instances within a
program, the debugging function could say something like, "Hey, the
foo object is the one that's hosed up" at runtime, making
troubleshooting the problem much easier.

In the end, debugging can only be an manual incode affair, setting
breakpoints or logpoints at strategically correct positions in the code.
and improving on that by intelligent analysis of repeated trial and
error.

Js is a scripting [or runtime compiling] and not a compiler language,
where the need for adding a symbol table at compiled debug time is much
stronger, as it's objects [variables, functions, etc.] are nameless at
user runtime.
 
J

John G Harris

On Mon, 3 Dec 2007 at 14:07:50, in comp.lang.javascript, TonyV wrote:

As most people who *actually* have knowledge about programming know,
almost every language maintains something called a "symbol table" that
associates names of things with what and where they are. When you say
something like:
var foo = 12;

the symbol table is what associates the memory location that holds the
value of 12 with the name "foo." Later, if you say:
foo += 4;

Javascript uses the symbol table to know what exactly is supposed to
be incremented by four. I know, this is all pretty sophisticated and
might seem to work like "magic" as far as you can tell. But I assure
you, it's *not* magic, it's *not* nonsense, and it usually works
pretty well behind the scenes with no need to worry much about it.
All I needed to know is if there's a way to get this information at
runtime, which from what I've read here, there's not. That's all,
question answered, no need to be condescending.
<snip>

I wish you'd said this earlier so we could explain what the problem is,
as follows :-


First, a javascript variable holds a value that is small and primitive,
whatever type it is. It never holds an object, whatever the appearance
of the code might suggest. Instead it holds a pointer to the object. The
object might be at a different address each time the program is run.
Seeing the value held in the variable is not going to be helpful : it's
just an address; an address that neither you nor the compiler can
predict.

Java is the same by the way.


Second, the official execution model for javascript says that variables
are implemented as properties of relevant system objects. An object's
properties are (name, value) structures held by the object in no
particular order. A variable needn't have a fixed address. It is found,
notionally at least, by searching for the name every time it is
accessed.

Where other languages have symbol tables inside the compiler linking
names and addresses, javascript has property collections inside the
executing program linking names and values.


It should now be clear that you really were asking the wrong question.

John
 

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

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top