this pointer

E

exoscale

in the following script ~this pointer~(self variable) is used to bind
XHR to the object that calls the function. but i am wondering why it
is required. anonymous function assigned to the onreadystatechange
event method could create a closure that would make xmlHttpReq
accessible even XHR is not bound to anything, i've tested this
situation if not made some mistake. XHR does not seem to be destroyed
when the function exits as its called asynchronously It does not make
sense in this simple code but may be meaningfull in an OO approach as
multiple functions access the XHR object. Am i right? So what is the
semantic of this usage?
thanks

function xmlhttpPost(strURL) {
var xmlHttpReq = false;
var self = this;
// Mozilla/Safari
if (window.XMLHttpRequest) {
self.xmlHttpReq = new XMLHttpRequest();
}
// IE
else if (window.ActiveXObject) {
self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-
www-form-urlencoded');
self.xmlHttpReq.onreadystatechange = function() {
if (self.xmlHttpReq.readyState == 4) {
updatepage(self.xmlHttpReq.responseText);
}
}
self.xmlHttpReq.send(getquerystring());
}


http://www.degraeve.com/reference/simple-ajax-example.php
 

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,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top