PDA

View Full Version : Accessing parent 'objects'


bradeck
01-23-2004, 05:03 AM
Given the following:

function Obj1()
{
this.elem1 = "blah";
this.Obj2 = Obj2;
}

function Obj2()
{
}

I can call Obj2 by stating:

Obj1.Obj2();

but how does one access elem1 from Obj2?

glenngv
01-23-2004, 05:54 AM
var myobj = new Obj1();
alert(myobj.elem1);
myobj.Obj2();

bradeck
01-23-2004, 06:26 AM
Close but not what I need. I need to access Obj1's elem1 member from Obj2, not from an instantiate object of Obj1:

function Obj2()
{
foo = this.elem1; // this is what a book says
this.elem1 = "newtest"; // but this fails with error...
}

glenngv
01-23-2004, 06:43 AM
This works for me:


function Obj1()
{
this.elem1 = "blah";
this.Obj2 = Obj2;
}

function Obj2()
{
alert(this.elem1); //alerts blah
this.elem1="foo";
alert(this.elem1);//alerts foo
}

var o = new Obj1();
o.Obj2();