E
exoscale
in the following script we know ~this~(self variable) is used to bind
XHR to the object that calls the function. but why is it required?
anonymous function assigned to the onreadystatechange event method
could create a closure that would make it xmlHttpReq accessible even
XHR is not bound to anything, i've tested it if not made some mistake.
It does not make sense in this simple code but may have some usage in
a large application. 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
XHR to the object that calls the function. but why is it required?
anonymous function assigned to the onreadystatechange event method
could create a closure that would make it xmlHttpReq accessible even
XHR is not bound to anything, i've tested it if not made some mistake.
It does not make sense in this simple code but may have some usage in
a large application. 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