Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically determining the variable name of an object instance

Status
Not open for further replies.

Syaodzir

Programmer
Oct 25, 2000
22
0
0
GB
Is it possible to dynamically determine the variable name of an object instance, e.g,

I create an instance of my object:

NewObject = new MyObject();

Within the constructor i want to be able to determine that the instance is called "NewObject":

function MyObject()
{
this.name = code_to_determine_my_name;
}

This should evaluate to MyObject.name == "NewObject"

I could of course pass the name as a parameter to the constructor but am trying to avoid this.

Anyone know how to accomplish this?

tnks in advance. lachlan@cynaptic.net
 
hie
what is the point?
if u dont want 2 pass a variable, use global one or global array of ones or generate the name randomly..

regards, vic
 
The point is to make the object have the ability to determine everything itself.

The object should not have to rely on external input like passing the instance variable name or creating global arrays.

These are options but not what i am looking for.

tnks anyway.
EMail: lgworks at uklinux.net
 
Try the MyObject.name attribute. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
don't get quite what you want...isn't the name of the variable just a reference to the object? i think the actual name property is only relevant for objects with names e.g.
Code:
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function getName(obj)
{
    alert(obj.name);
}
</SCRIPT>
<INPUT TYPE=&quot;text&quot; NAME=&quot;input1&quot; onChange=&quot;getName(this);&quot;>

but if you created say a date object, there is no such thing as a name attribute. can you give an example of what you want to do with it? ~ ~
 
Example:

// prototype method
function _Pointless_Inc()
{
this.x++;
setTimeout(this.name + &quot;.Inc()&quot;, 1000);
//will cause an error
}


// constructor

function Pointless()
{
if(!Pointless.prototype.Inc) {
Pointless.prototype.Inc = _Pointless_Inc;
}
this.x = 0;
}


This will fail because this.name is not defined.

The way i dont want to fix this is to change the constructor to look like this(where myName is the name of the variable passed to the constructor):

function Pointless(myName)
{
if(!Pointless.prototype.Inc) {
Pointless.prototype.Inc = _Pointless_Inc;
}
this.x = 0;
this.name = myName;
}


This requires outside interaction which is what i am trying to avoid.

Instead it should have this general structure without the myName parameter:

function Pointless()
{
if(!Pointless.prototype.Inc) {
Pointless.prototype.Inc = _Pointless_Inc;
}
this.x = 0;
this.name =
code_to_determine_my_name;
}


I guess i was just hoping that there was a way for an instance to get it's variable name through some core javascript mechanisim so that it could be completely self referencing without outside intervention.

tnks anyhoo guys.
EMail: lgworks at uklinux.net
 
wait, i cant get it, do u want 2 generate this name on-the-fly or it is PREdefined somewhere somehow? regards, vic
 
Using the above example, if i was to pass the name as a parameter then the creation of the instance would look like this:

var aPointlessVar = new Pointless(&quot;aPointlessVar&quot;);

then in the constructor this.name = myName is equivalent to this.name = &quot;aPointlessVar&quot; so that the method code setTimeout(this.name + &quot;.Inc()&quot;,1000) is equivalent to setTimeout(&quot;aPointlessVar.Inc()&quot;, 1000).

I just want to everything to be determined from within the constructor so that it will know it is called &quot;aPointlessVar&quot; without passing the information as a parameter.
EMail: lgworks at uklinux.net
 
ok, does this demonstrate the problem...the this keyword is no longer refers to the input when you put it inside the setTimeOut thing?

Code:
<HTML><body>
<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
function getName(obj)
{
    alert(obj.name);
}
</SCRIPT>
<FORM name=&quot;form1&quot;>
works: <INPUT TYPE=&quot;text&quot; NAME=&quot;input1&quot; onChange=&quot;getName(this)&quot;><BR>
doesnt:<INPUT TYPE=&quot;text&quot; NAME=&quot;input2&quot; onChange=&quot;setTimeout('getName(this)',1000);&quot;><BR>
works but need name:<INPUT TYPE=&quot;text&quot; NAME=&quot;input3&quot; onChange=&quot;setTimeout('getName(document.form1.input3)',1000);&quot;>
</FORM>
</body></html>

if so, hmm thats an interesting problem, normally passing this is ok, but in this case you're just passing a string...um, if that is the problem, can't think of an answer right now, but i'll keep thinking :) ~ ~
 
i got ur point, it really doesnt work with setTimeout.. weird
well, here is some workaround, but u wuld have 2 modify it a bit for multiple calling..
<html><head><title>tryin</title></head>
<script>
function holder(){ this.currentObject=0 }
var temp=new holder()

// prototype method
function _Pointless_Inc(){
temp.currentObject=this
this.x++;
setTimeout(&quot;temp.currentObject.Inc()&quot;, 3000); }

// constructor
function Pointless(){
if(!Pointless.prototype.Inc) {
Pointless.prototype.Inc = _Pointless_Inc; }
this.x = 0;}

var aaa=new Pointless()
aaa.Inc()

</script>
<body bgcolor=&quot;#FFFFFF&quot;></body></html>
regards, vic
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top