event handler scope inside object method

T

trib

Hi,

I'm reasonably new to OO javascript, but appreciating it's versatility.
I'm trying to do an XMLHTTP request from an instance method of a
object defined by a class, and I would normally embed the response
handler in the function, like so:

var req = newXmlHttp();
req.open("GET", url, true);

// When the response is received, process it
req.onreadystatechange = function() {
if (req.readyState == 4) {
..etc...
}
}

But, if req is an instance property, and is referenced as this.req, it
does not seem to be available within the scope of the inline function.
As a workaround I'm currently defining the handler function as a
separate instance method, and setting up the event handler like so:

var thisobj= this;
this.req.onreadystatechange = function () { thisobj.processResponse()
};

Is there any way to keep it as an inline function?

Cheers,

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top