I am still not clear about how to reference an object within another
object to pass first object to a function:
var Parent = {
myFunc : function(){
alert("Parent = "+this)
},
Child : {
//how to get reference to Parent?
myChildFunc : function(){
alert("Parent IS NOT "+this);
this.myFunc(); //error -> this.myFunc is not a function
}
},
}
window.onload = Parent.Child.myChildFunc;
|
|
0
|
|
|
|
Reply
|
vunet.us (106)
|
9/16/2008 3:29:08 PM |
|
vunet <vunet.us@gmail.com> writes:
> I am still not clear about how to reference an object within another
> object to pass first object to a function:
>
> var Parent = {
> myFunc : function(){
> alert("Parent = "+this)
> },
> Child : {
> //how to get reference to Parent?
> myChildFunc : function(){
> alert("Parent IS NOT "+this);
> this.myFunc(); //error -> this.myFunc is not a function
> }
> },
>
> }
> window.onload = Parent.Child.myChildFunc;
The way it's written, the function won't even know the "Child" object
when it's called.
The same function can be a property of many objects at the same time
(as can any other object). There is no way to go from the function
code to anything but the object it was called as a method of (i.e.,
"this"). The references go in the other direction.
/L
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
|
|
0
|
|
|
|
Reply
|
Lasse
|
9/16/2008 6:02:40 PM
|
|
On Sep 16, 2:02=A0pm, Lasse Reichstein Nielsen <lrn.unr...@gmail.com>
wrote:
> vunet <vunet...@gmail.com> writes:
> > I am still not clear about how to reference an object within another
> > object to pass first object to a function:
>
> > var Parent =3D {
> > =A0 myFunc : function(){
> > =A0 =A0 alert("Parent =3D "+this)
> > =A0 },
> > =A0 Child : {
> > =A0 =A0 //how to get reference to Parent?
> > =A0 =A0 myChildFunc : function(){
> > =A0 =A0 =A0 =A0alert("Parent IS NOT "+this);
> > =A0 =A0 =A0 =A0this.myFunc(); //error -> this.myFunc is not a function
> > =A0 =A0 }
> > =A0 },
>
> > }
> > window.onload =3D Parent.Child.myChildFunc;
>
> The way it's written, the function won't even know the "Child" object
> when it's called.
>
> The same function can be a property of many objects at the same time
> (as can any other object). There is no way to go from the function
> code to anything but the object it was called as a method of (i.e.,
> "this"). The references go in the other direction.
>
> /L
> --
> Lasse Reichstein Nielsen
> =A0DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.=
html>
> =A0 'Faith without judgement merely degrades the spirit divine.'
So how would I write the function within an object B which is within
an object A, which (i.e. the function) would would have a reference to
object A methods?
|
|
0
|
|
|
|
Reply
|
vunet
|
9/16/2008 7:15:34 PM
|
|
On Sep 16, 3:15=A0pm, vunet <vunet...@gmail.com> wrote:
> On Sep 16, 2:02=A0pm, Lasse Reichstein Nielsen <lrn.unr...@gmail.com>
> wrote:
>
>
>
> > vunet <vunet...@gmail.com> writes:
> > > I am still not clear about how to reference an object within another
> > > object to pass first object to a function:
>
> > > var Parent =3D {
> > > =A0 myFunc : function(){
> > > =A0 =A0 alert("Parent =3D "+this)
> > > =A0 },
> > > =A0 Child : {
> > > =A0 =A0 //how to get reference to Parent?
> > > =A0 =A0 myChildFunc : function(){
> > > =A0 =A0 =A0 =A0alert("Parent IS NOT "+this);
> > > =A0 =A0 =A0 =A0this.myFunc(); //error -> this.myFunc is not a functio=
n
> > > =A0 =A0 }
> > > =A0 },
>
> > > }
> > > window.onload =3D Parent.Child.myChildFunc;
>
> > The way it's written, the function won't even know the "Child" object
> > when it's called.
>
> > The same function can be a property of many objects at the same time
> > (as can any other object). There is no way to go from the function
> > code to anything but the object it was called as a method of (i.e.,
> > "this"). The references go in the other direction.
>
> > /L
> > --
> > Lasse Reichstein Nielsen
> > =A0DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDO=
M.html>
> > =A0 'Faith without judgement merely degrades the spirit divine.'
>
> So how would I write the function within an object B which is within
> an object A, which (i.e. the function) would would have a reference to
> object A methods?
Details of what I need:
var A =3D {
B : {
f : function(){
this.Fn(); // <=3D=3D ERROR REFERENCING, HELP!
//I understand "this" in this case is a reference to object "B"
not "A"
//How to get reference to A?
}
},
Fn : function(){
alert("Hey!");
}
}
|
|
0
|
|
|
|
Reply
|
vunet
|
9/16/2008 7:28:12 PM
|
|
vunet <vunet.us@gmail.com> writes:
> Details of what I need:
>
> var A = {
> B : {
> f : function(){
> this.Fn(); // <== ERROR REFERENCING, HELP!
> //I understand "this" in this case is a reference to object "B"
> not "A"
correct, at least, if you call it as A.B.f() - there are plenty of ways
to call that function where this may refer to something else completely,
including A.
> //How to get reference to A?
see below.
> }
> },
> Fn : function(){
> alert("Hey!");
> }
> }
You can't get a reference to A from B, unless you create a reference
yourself. You can think of properties as analogous to singly linked
lists. You can get from A to B, but there is no reference back.
You may want something like:
var A = {
B : {
f : function(){
this.parent.Fn();
}
},
Fn : function(){
alert("Hey!");
}
}
A.B.parent = A;
A.B.f();
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
|
|
0
|
|
|
|
Reply
|
Joost
|
9/16/2008 9:44:47 PM
|
|
On Sep 16, 8:29=A0am, vunet <vunet...@gmail.com> wrote:
> I am still not clear about how to reference an object within another
> object to pass first object to a function:
>
> var Parent =3D {
> =A0 myFunc : function(){
> =A0 =A0 alert("Parent =3D "+this)
> =A0 },
> =A0 Child : {
> =A0 =A0 //how to get reference to Parent?
> =A0 =A0 myChildFunc : function(){
> =A0 =A0 =A0 =A0alert("Parent IS NOT "+this);
> =A0 =A0 =A0 =A0this.myFunc(); //error -> this.myFunc is not a function
> =A0 =A0 }
> =A0 },
>
> }
>
> window.onload =3D Parent.Child.myChildFunc;
Something like this might work for you.
function Parent() {
this.fruit =3D 'apple'
parentObj =3D this;
this.myFunc =3D function(){
alert("Fruit =3D "+this.fruit);
};
this.Child =3D {
myChildFunc : function(){
parentObj.myFunc();
}
};
}
window.onload =3D new Parent().Child.myChildFunc;
Steve
http://webstervanrobot.com/
|
|
0
|
|
|
|
Reply
|
showellshowell
|
9/17/2008 2:25:43 AM
|
|
On Sep 16, 10:25=A0pm, "showellshow...@gmail.com"
<showellshow...@gmail.com> wrote:
> On Sep 16, 8:29=A0am, vunet <vunet...@gmail.com> wrote:
>
>
>
> > I am still not clear about how to reference an object within another
> > object to pass first object to a function:
>
> > var Parent =3D {
> > =A0 myFunc : function(){
> > =A0 =A0 alert("Parent =3D "+this)
> > =A0 },
> > =A0 Child : {
> > =A0 =A0 //how to get reference to Parent?
> > =A0 =A0 myChildFunc : function(){
> > =A0 =A0 =A0 =A0alert("Parent IS NOT "+this);
> > =A0 =A0 =A0 =A0this.myFunc(); //error -> this.myFunc is not a function
> > =A0 =A0 }
> > =A0 },
>
> > }
>
> > window.onload =3D Parent.Child.myChildFunc;
>
> Something like this might work for you.
>
> function Parent() {
> =A0 this.fruit =3D 'apple'
>
> =A0 parentObj =3D this;
> =A0 this.myFunc =3D function(){
> =A0 =A0 alert("Fruit =3D "+this.fruit);
> =A0 };
> =A0 this.Child =3D {
> =A0 =A0 myChildFunc : function(){
> =A0 =A0 =A0 parentObj.myFunc();
> =A0 =A0 }
> =A0 };
>
> }
>
> window.onload =3D new Parent().Child.myChildFunc;
>
> Stevehttp://webstervanrobot.com/
Interesting. So when initializing a function as parent of objects it
kinda works but having a global object as parent does not... My idea
was to separate objects' logic within one global parent object but it
looks like I need to apply those references or convert them into
function objects.
Thanks
|
|
0
|
|
|
|
Reply
|
vunet
|
9/17/2008 2:11:46 PM
|
|
On Sep 17, 3:28 am, vunet <vunet...@gmail.com> wrote:
> On Sep 16, 3:15 pm, vunet <vunet...@gmail.com> wrote:
> > So how would I write the function within an object B which is within
> > an object A, which (i.e. the function) would would have a reference to
> > object A methods?
>
> Details of what I need:
>
> var A = {
> B : {
> f : function(){
> this.Fn(); // <== ERROR REFERENCING, HELP!
> //I understand "this" in this case is a reference to object "B"
> not "A"
> //How to get reference to A?
> }
> },
> Fn : function(){
> alert("Hey!");
> }
>
> }
Just reference A directly:
var A = {
B : {
f : function () {
A.Fn(); // <-----
}
},
Fn : function () {
alert("Hey!");
}
}
Btw, using 'this' in javascript may or may not do what you want:
A.B.f() // 'this' refers to A.B
document.body.onclick = A.B.f // 'this' refers to document.body
|
|
0
|
|
|
|
Reply
|
slebetman
|
9/18/2008 1:29:01 AM
|
|
|
7 Replies
93 Views
(page loaded in 0.061 seconds)
|