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

J

Jeremy Wray

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

Martin Honnen

Jeremy said:
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.

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()
 
D

Douglas Crockford

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):

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.crockford.com/#javascript
 

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

Staff online

Members online

Forum statistics

Threads
474,079
Messages
2,570,574
Members
47,206
Latest member
Zenden

Latest Threads

Top