[Javascript] Please help: problem with Javascript 'this' keyword in nested objects!

Dan Costea costea.dan at ssi-schaefer.ro
Mon Apr 29 11:18:06 CDT 2002


I'm not sure I understud exactly your problem. I think you should give an
example. But, if your problem is that you cannot use objects as attributes
of another objects, than here is an example:



function Object1 ()
{
 this.obj1_attr1 = "";
 this.obj1_attr2 = "";

 this.obj1_constr = obj1_constr;
}

function obj1_constr (attr1, attr2)
{
 this.obj1_attr1 = attr1;
 this.obj1_attr2 = attr2;
}


function Object2 ()
{
 this.obj2_attr1 = "";
 this.obj2_attr2 = "";
 this.obj2_obj1 = "";

 this.obj2_constr = obj2_constr;
 this.obj2_constr ("object 2; attribute 1", "object 2; attribute 2");
}

function obj2_constr (attr1, attr2)
{
 this.obj2_attr1 = attr1;
 this.obj2_attr2 = attr2;

 this.obj2_obj1 = new Object1 ();
 this.obj2_obj1.obj1_constr ("object 1; attribute 1", "object 1; attribute
2");
}

function testing ()
{
 var testObj = new Object2 ();
 alert ("testObj.obj2_attr1 =\t\t" + testObj.obj2_attr1 +
   "\ntestObj.obj2_attr2 =\t\t" + testObj.obj2_attr2 +
   "\ntestObj.obj2_obj1.obj1_attr1 =\t" + testObj.obj2_obj1.obj1_attr1 +
   "\ntestObj.obj2_obj1.obj1_attr2 =\t" + testObj.obj2_obj1.obj1_attr2);
}


Dan

----- Original Message -----
From: "Amanda Birmingham" <lists at imladris.com>
To: <Javascript at LaTech.edu>
Sent: Friday, April 26, 2002 9:17 AM
Subject: [Javascript] Please help: problem with Javascript 'this' keyword in
nested objects!


> Hello, List,
> I apologize to anyone who has seen this question on the wdvltalk list, but
> I'm desperate for an answer in short order ...
>
> I am having a serious *issue* with the @#$%& 'this' keyword.  Basically, I
> have a setup like this: I have two types of objects defined.  Object1 has
a
> constructor that defines some methods for Object1, and these methods use
> the 'this' keyword to refer to properties of Object1.  Object2 has a
> constructor that creates a new instance of Object1 as one of its
> properties.  The Object2 then attempts to use one of the methods of its
new
> Object1 property by calling "this.Object1.methodName".
>
> Which flunks.  It flunks because methodName uses the "this" keyword inside
> it, and expects "this" to refer to an object of type
> Object1.  Unfortunately, for reasons I totally cannot fathom, when I call
> methodName as described above, the "this" it is using is the "this" for
> Object2 (the outside one).  Of course, Object2 doesn't have a bunch of
> properties that Object1 does, so methodName flunks.
>
> Why the heck is this happening?! (I have verified that it actually
> *is*).  How do I write my methods to access the properties of their
> immediate "parent", rather than the properties of the object that
> ultimately invoked them?? (short of using "this.Object1" in place of
"this"
> in methodName, which is not an option because it makes Object1 completely
> un-reusable.)
>
> I hope I've managed to describe the issue in a way that is at least
> somewhat clear--if not, please let me know and I can send pseudocode.  Any
> suggestions would be REALLY welcome, as this problem is keeping me from
> getting anything done until I know how to get around it!
>
> Thanks as always,
>
> Amanda Birmingham
> Web Application Developer
>
> _______________________________________________
> Javascript mailing list
> Javascript at LaTech.edu
> https://lists.LaTech.edu/mailman/listinfo/javascript




More information about the Javascript mailing list