470,620 Members | 1,441 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,620 developers. It's quick & easy.

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 !=
this.req.open("GET",url, true);

function MyProcess (){

this.aSuggestions = [];

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

All seems to work fine, except that this.req is null when MyProcess is
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 1184

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();
// ...


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

Nov 30 '05 #2

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

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

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:-


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

However if you just call


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


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"

The solution provided above uses closures.

Read this article on closures:-



Julian Turner

Nov 30 '05 #3

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by TPJ | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.