By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,516 Members | 1,137 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,516 IT Pros & Developers. It's quick & easy.

Object Reference Within Itself

P: n/a
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;
Sep 16 '08 #1
Share this Question
Share on Google+
7 Replies


P: n/a
vunet <vu******@gmail.comwrites:
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.'
Sep 16 '08 #2

P: n/a
On Sep 16, 2:02*pm, Lasse Reichstein Nielsen <lrn.unr...@gmail.com>
wrote:
vunet <vunet...@gmail.comwrites:
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.'
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?
Sep 16 '08 #3

P: n/a
On Sep 16, 3:15*pm, vunet <vunet...@gmail.comwrote:
On Sep 16, 2:02*pm, Lasse Reichstein Nielsen <lrn.unr...@gmail.com>
wrote:
vunet <vunet...@gmail.comwrites:
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.'

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!");
}
}
Sep 16 '08 #4

P: n/a
vunet <vu******@gmail.comwrites:
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/
Sep 16 '08 #5

P: n/a
On Sep 16, 8:29*am, vunet <vunet...@gmail.comwrote:
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;
Something like this might work for you.

function Parent() {
this.fruit = 'apple'

parentObj = this;
this.myFunc = function(){
alert("Fruit = "+this.fruit);
};
this.Child = {
myChildFunc : function(){
parentObj.myFunc();
}
};
}

window.onload = new Parent().Child.myChildFunc;

Steve
http://webstervanrobot.com/

Sep 17 '08 #6

P: n/a
On Sep 16, 10:25*pm, "showellshow...@gmail.com"
<showellshow...@gmail.comwrote:
On Sep 16, 8:29*am, vunet <vunet...@gmail.comwrote:
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;

Something like this might work for you.

function Parent() {
* this.fruit = 'apple'

* parentObj = this;
* this.myFunc = function(){
* * alert("Fruit = "+this.fruit);
* };
* this.Child = {
* * myChildFunc : function(){
* * * parentObj.myFunc();
* * }
* };

}

window.onload = 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
Sep 17 '08 #7

P: n/a
On Sep 17, 3:28 am, vunet <vunet...@gmail.comwrote:
On Sep 16, 3:15 pm, vunet <vunet...@gmail.comwrote:
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
Sep 18 '08 #8

This discussion thread is closed

Replies have been disabled for this discussion.