473,320 Members | 1,879 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Object Reference Within Itself

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
7 2363
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Daniel | last post by:
Hello =) I have an object which contains a method that should execute every x ms. I can use setInterval inside the object construct like this - self.setInterval('ObjectName.methodName()',...
6
by: Martin | last post by:
I'd like to be able to get the name of an object instance from within a call to a method of that same object. Is this at all possible? The example below works by passing in the name of the object...
29
by: web1110 | last post by:
If I have 2 variables, A and B, referencing the same object and then do a A.Dispose(), what happens to B?
2
by: bughunter | last post by:
This is partly 'for the record' and partly a query about whether the following is a bug somewhere in .Net (whether it be the CLR, JITter, C# compiler). This is all in the context of .Net 1.1 SP1. ...
12
by: Andrew Poulos | last post by:
With the following code I can't understand why this.num keeps incrementing each time I create a new instance of Foo. For each instance I'm expecting this.num to alert as 1 but keeps incrementing. ...
28
by: ensemble | last post by:
I'm trying to utilized a more object-oriented approach to managing window events in javascript. Thus, I am creating a "controller" object to handle events and interact with the server. However, I...
4
by: gg9h0st | last post by:
i worte a simple code below. ------------------------------------------------------------------------------------ #include "stdafx.h" class Object { public: int a;
5
by: reycri | last post by:
Hi, I need to be able to do this: var func = new Function("var me = <selfRef>; alert(me.params);"); func.params = "This is a test parameter"; window.setTimeout(func, 500); Basically, I...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.