472,989 Members | 3,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Problem with object instance....

I'm new to JavaScript and this is annoying me. I have defined a "class"
(JavaScript OO seems really strange to me...) in the following way:

function StateSuggestions(pSource) {
this.source = pSource;

this.req=new ActiveXObject("Msxml2.XMLHTTP"); //this.req != null
this.Process = MyProcess;
this.requestSuggestions = MyRequestSuggestions;
....

//requestSuggestions is called from Outside
function MyRequestSuggestions(pAutoSuggestControl, pTypeAhead) {

this.req.onreadystatechange = this.Process; //this.req !=
null
this.req.open("GET",url, true);
....

function MyProcess (){

this.aSuggestions = [];

if (this.req!=null){ //this.req ==
null......why??????
if (this.req.readyState == 4){
if (this.req.status == 200){
....

All seems to work fine, except that this.req is null when MyProcess is
reached...
When creating the object this.req != null, in MyRequestSuggestions
this.req != null, but when MyProcess is reached, this.req turns to null

Best Regards
Fabio Cavassini

Nov 29 '05 #1
3 1250
VK

Fabio Cavassini wrote:
I'm new to JavaScript and this is annoying me. I have defined a "class"
(JavaScript OO seems really strange to me...) in the following way:


Well, your definition is rather strange from the point of view of any
OOP language ;-)

If you vant to assign a function result, you use:
this.member = someFunction();

If you vant to assign a new object instance, you use:
this.member = new someFunction();

In the listed example you assign a constructor reference to your member
which is rather pointless except some really special occasions.
Presuming I decrypted your original intentions properly:

function StateSuggestions(pSource) {
this.source = pSource;
this.req=new ActiveXObject("Msxml2.XMLHTTP"); // > IE 5.x only !
this.Process = new MyProcess();
this.requestSuggestions = new MyRequestSuggestions();
// ...
}

<snip>

At this point I seem having lost the thread. You know, irrelevant to
JavaScript specifics you have to take a decision who is who in your
object. Say "Object A has instances of object B ans C as its members";
or: "Object C has instances of object A ans B as its members".

The situation when "Object A has an instance of B as a member and B has
an instance of A as a member" are not in common use unless you writing
a "Hacking OOP" book. Could you post a block-scheme of the desired
structure?

Nov 30 '05 #2

Fabio Cavassini wrote:
[snip]
this.req.onreadystatechange = this.Process; //this.req !=


AFAIK, the problem is here, and your understanding of the "this"
keyword.

Try instead:-

var INSTANCE=this;

this.req.onreadystatechange = function() {INSTANCE.Process();};

In rough terms, the value of "this" within a function depends on what
is calling the function.

If I have:-

myObjInstance.Process()

then "this" is set to "myObjInstance" within the Process function.

However if you just call

Process()

then "this" refers to the window object in the browser. I.e. it is as
if you called:-

window.Process()

In your code, the effect of the assignment expression

this.req.onreadystatechange = this.Process;

Is to pass a reference to the "Process" function alone, which does not
include the "this." context.

Accordingly when "onreadystatechange" is fired, is is effectively
calling "Process()", not "obj.Process()".

So "this" will point to the window object, which does not have a "req"
property.

The solution provided above uses closures.

Read this article on closures:-

<URL:http://jibbering.com/faq/faq_notes/closures.html>

Regards

Julian Turner

Nov 30 '05 #3
Greeaattt!

Not only it works perfectly, now I understand why, thanks Julian ;)

This code is part of a "Google Suggest" like implementation, let me
know if you are interested in it...

Best Regards

Nov 30 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
0
by: john | last post by:
Hi,All Gurus: It is kind of complicated, please bear with me and let me know if you have any questions. Thanks a lot in advance. John I have a csharp method, using emit to dynamically generate...
10
by: Opa | last post by:
I have tried for two days to solve this problem with no luck. I have a singleton class which has some events declared. When I inherit from this class the events don't seem to come along with it....
0
by: john | last post by:
Hi,All Gurus: It is kind of complicated, please bear with me and let me know if you have any questions. Thanks a lot in advance. John I have a csharp method, using emit to dynamically generate...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.