CHAPTER 2 THE NUTS AND BOLTS OF (My web server)

CHAPTER 2 THE NUTS AND BOLTS OF AJAX 39 The Magic of the Asynchronous Class Let s focus on how the Asynchronous class solves the instance and callback problem. The specific code is illustrated again as follows: function Asynchronous_call(url) { var instance = this; this._xmlhttp.open(’GET’, url, true); this._xmlhttp.onreadystatechange = function() { switch(instance._xmlhttp.readyState) { case 1: instance.loading(); break; case 2: instance.loaded(); break; case 3: instance.interactive(); break; case 4: instance.complete(instance._xmlhttp.status, instance._xmlhttp.statusText, instance._xmlhttp.responseText, instance._xmlhttp.responseXML); break; } } this._xmlhttp.send(null); } Asynchronous_call is associated with an instance of Asynchronous because of the prototype definition. Then when the HTML code calls asynchronous.call, the function Asynchronous_callis called and the this instance references the instantiated class. The variable this.xmlhttp is an instance of XMLHttpRequest, and the property onreadystatechange needs to be assigned a function. There is a peculiarity with JavaScript in that if a property is assigned the value of this.somefunction, then what is assigned is a function and not a function associated with a class instance, as was shown by the code that looked like it would work, but didn t. When the method Asynchronous_call is called, the this variable references an instance of Asynchronous. What is happening is that JavaScript is associating a function with an instance. Logically then, if the property onreadystatechange were assigning a function associated with an instance of Asynchronous, then when a callback is made, the this variable should reference an instance of Asynchronous. Figure 2-7 shows that there is no reference to an instance of Asynchronous.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

Leave a Reply