User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 426,412 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,366 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 2091 | Replies: 3
Reply
Join Date: Jun 2007
Posts: 2
Reputation: exoscale is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
exoscale exoscale is offline Offline
Newbie Poster

this pointer

  #1  
Jun 20th, 2007
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 methods access the XHR object. Am i right? So what is the semantic of this usage?
thanks

  1. function xmlhttpPost(strURL) {
  2. var xmlHttpReq = false;
  3. var self = this;
  4. // Mozilla/Safari
  5. if (window.XMLHttpRequest) {
  6. self.xmlHttpReq = new XMLHttpRequest();
  7. }
  8. // IE
  9. else if (window.ActiveXObject) {
  10. self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  11. }
  12. self.xmlHttpReq.open('POST', strURL, true);
  13. self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-
  14. www-form-urlencoded');
  15. self.xmlHttpReq.onreadystatechange = function() {
  16. if (self.xmlHttpReq.readyState == 4) {
  17. updatepage(self.xmlHttpReq.responseText);
  18. }
  19. }
  20. self.xmlHttpReq.send(getquerystring());
  21. }
http://www.degraeve.com/reference/si...ax-example.php
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,858
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: this pointer

  #2  
Jun 21st, 2007
The this pointer is not compulsory. Try it out without the 'this' and it would still work out to be fine. Since this function is executed in the context of some form element (text box in the example given below), 'this' here refers to the element under consideration.

Read this for more on this in javascript.

  1. function ajaxFunction(strURL) {
  2. var xmlHttpReq = false;
  3. var self = this;
  4. // Mozilla/Safari
  5. if (window.XMLHttpRequest) {
  6. self.xmlHttpReq = new XMLHttpRequest();
  7. }
  8. // IE
  9. else if (window.ActiveXObject) {
  10. self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
  11. }
  12. self.xmlHttpReq.open('POST', strURL, true);
  13. self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-
  14. www-form-urlencoded');
  15. self.xmlHttpReq.onreadystatechange = function() {
  16. if (self.xmlHttpReq.readyState == 4) {
  17. updatepage(self.xmlHttpReq.responseText);
  18. }
  19. }
  20. self.xmlHttpReq.send(getquerystring());
  21. }
  22.  
  23. <form name="myForm">
  24. Name: <input type="text" onkeyup="ajaxFunction(time.jsp);" name="username" />
  25. Time: <input type="text" name="time" />
  26. </form>
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Jun 2007
Posts: 2
Reputation: exoscale is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
exoscale exoscale is offline Offline
Newbie Poster

Re: this pointer

  #3  
Jun 21st, 2007
thanks for the answer SOS. but the onkeyup just calls the function in your example code. so -this- refers to the global (window) object, not the textbox. as i stated before i know that it is not required to bind the XHR to some object by using this. however i've remembered when referred to quirksmode article that -this- in the example does not bind XHR to the object which invokes the function, but binds it to the global object (window).
in this case, xmlHttpReq becomes a global variable although not defined as a global one. it is arguable that it would make sense or not.

i finally conclude that this sample is crippled from a working code that everything makes sense but removed parts makes -this- usage not so meaningful in the example code.
Reply With Quote  
Join Date: Jul 2006
Location: Deptford, London
Posts: 963
Reputation: MattEvans has a spectacular aura about MattEvans has a spectacular aura about 
Rep Power: 5
Solved Threads: 48
Moderator
Featured Poster
MattEvans's Avatar
MattEvans MattEvans is online now Online
Posting Shark

Re: this pointer

  #4  
Jun 22nd, 2007
If Javascript's object lifecycle is anything like Java's; objects created using 'new' stay resident even after they go out of the scope of the function that called new, provided something else is referencing that object somewhere.. perhaps, the XMLHTTP objects do some kind of forced binding to a single static controller object whenever open() is called... even if they don't; try:

  1. function Thing()
  2. {
  3. var myself = this;
  4. window.setTimeout(function(){myself.say_something();},100);
  5. this.say = "hello there";
  6. this.say_something = function()
  7. {
  8. alert( this.say );
  9. }
  10. }
  11.  
  12. function something()
  13. {
  14. thing = new Thing( );
  15. return;
  16. }
  17.  
  18. something( );
  19. alert('did something');

In Opera, I get 'did something' and then 'hello there'.

There's a very weak binding in that the 'myself' provided in the function created for the ( also asyncrounous ) window.setTimeout( ) function refers to the instantiated 'Thing'. That's clearly enough to keep the object alive for at least until the timeout clicks... I'm sure there is some such similar behind-the-scenes binding going on somewhere in XMLHTTP.open( ), because the XMLHTTP has to be 'connected' (pardon the pun) to something that calls its onreadystatechange when the readystate changes; that will keep it from being GC'd.
If it only works in Internet Explorer; it doesn't work.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 1:46 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC