AJAX Forums

javascript : this pointer?

This is a discussion on javascript : this pointer? within the JavaScript forums, part of the Beginners AJAX category; 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 ...


Go Back   AJAX Forums > Beginners AJAX > JavaScript

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
Old 06-21-2007, 07:36 AM   #1 (permalink)
 
Join Date: Jun 2007
Posts: 0
Rep Power: 0 exoscale is on a distinguished road
javascript : this pointer?

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 self = this; self.xmlHttpReq.onreadystatechange = function() { if (self.xmlHttpReq.readyState == 4) { updatepage(self.xmlHttpReq.responseText); } } }http://www.degraeve.com/reference/si...ax-example.php
__________________
exoscale is offline   Reply With Quote
Old 06-21-2007, 09:10 PM   #2 (permalink)
 
Join Date: Jun 2007
Posts: 1
Rep Power: 0 Wyatt is on a distinguished road
i'm not sure i'm following your question completely, but self=this; creates a local scope instance of "this" so when the function exits, "this" should be unchanged, which is what i think you said is the behavior you are experiencing.
__________________
Wyatt is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 
Thread Tools
Display Modes



All times are GMT -4. The time now is 02:42 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5
Copyright ©2006 - 2008, AJAXwith.com