How do I get the name of my variable or object?

How do I get the name of my variable or object?

Post by Jeremy Wra » Fri, 15 Aug 2003 19:18:46


Hello

On the surface this would seem quite simple, but I'm blowed if I can find a
way to do it.

Let's say I create a variable:

var my_object = new Object();

now, I want to find out the name of my object, eg. "my_object". How do I do
this? To make things clearer, say I want to alert out the name of my object,
I might try something like:

alert(my_object.name):

this doesn't work, however. Any help appreciated...Jeremy.
 
 
 

How do I get the name of my variable or object?

Post by Martin Hon » Fri, 15 Aug 2003 19:30:08


An object doesn't have a name, inside of the methods of an object you
simply refer to
this.property
to access the object's properties. Other than that there is no name of
an object and there can't be, what would you suggest the name to be with
a code of
var obj0, obj1;
obj1 = obj = new Object()

--

Martin Honnen
http://www.yqcomputer.com/

 
 
 

How do I get the name of my variable or object?

Post by Douglas Cr » Fri, 15 Aug 2003 22:05:08

> Let's say I create a variable:

You can create your own constructor and pass the name to it.

function MyObject(name) {
this.name = name;
window[name] = this;
}

new MyObject('my_object');

alert(my_object.name);

http://www.yqcomputer.com/ #javascript