473,836 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.my ChildFunc;
Sep 16 '08 #1
7 2385
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.my ChildFunc;
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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Sep 16 '08 #2
On Sep 16, 2:02*pm, Lasse Reichstein Nielsen <lrn.unr...@gma il.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.my ChildFunc;

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/rasterTriangleD OM.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...@gma il.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.my ChildFunc;
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/rasterTriangleD OM.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.my ChildFunc;
Something like this might work for you.

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

parentObj = this;
this.myFunc = function(){
alert("Fruit = "+this.frui t);
};
this.Child = {
myChildFunc : function(){
parentObj.myFun c();
}
};
}

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.my ChildFunc;

Something like this might work for you.

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

* parentObj = this;
* this.myFunc = function(){
* * alert("Fruit = "+this.frui t);
* };
* this.Child = {
* * myChildFunc : function(){
* * * parentObj.myFun c();
* * }
* };

}

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.o nclick = 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
20358
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()', this.pinginterval); - but is there no way to do this without using the literal ObjectName? If I write 'this.methodName()' I get "Line 1 Char 1: Object doesn't support this property or method." in IE, and nothing happens in Firebird.
6
22542
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 instance (in this case 'myDog'). Of course it would be better if I could somehow know from within write() that the name of the object instance was 'myDog' without having to pass it as a parameter. //////////////////////////////// function...
29
2306
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
3198
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. Recently we (my fellow team members & I) observed an InvalidOperationException - 'collection has been modified', however it wasn't immediately obvious why this would be occuring. There are no modifications occuring within the loop and there is...
12
5562
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. Foo = function(type) { this.num = 0; this.type = type this.trigger(); } Foo.prototype.trigger = function() {
28
2134
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 apparently don't fully understand what "this" refers to. In the code below I was expecting that when the button was clicked (handleDeleteButtonClicked function) that "this" would be pointing at a ViewController object. Instead it was the Button...
4
1982
by: gg9h0st | last post by:
i worte a simple code below. ------------------------------------------------------------------------------------ #include "stdafx.h" class Object { public: int a;
5
2488
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 need to add properties to a function object and access them within the function when it is executing. Therefore, I need to be
275
12488
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
10844
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10591
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10254
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9374
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4452
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4016
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3115
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.