473,320 Members | 1,988 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Assigning methods to objects, and assigning onreadystatechange to an XMLHttpRequest -- an inconsistency?

So, the following apparently works as a method of grafting a method
onto an object (using the squarefree JS shell):

obj = { x: 1, inc: null }
result [object Object]
obj.inc = function () { this.x++ }
result function () { this.x++; }
obj.inc()
obj.x
result 2

And by works, I mean that when I create an object, and then assign one
of its properties an anonymous function, the "this" keyword within said
anyonymous function apparently refers to the object itself. Convenient.
I like it.

The problem is, in the context of an XMLHttpRequest object, this
doesn't seem to work as well for me. Again, using the squarefree js
shell:

xhr = new XMLHttpRequest()
result[object XMLHttpRequest]
xhr.onreadystatechange = function () { alert(this.responseText); }
resultfunction () { alert(this.responseText); }
xhr.open("GET","http://www.squarefree.com/shell/shell.html",true)

Now, one might think this would produce an alert containing the
text/html of shell.html. But it doesn't. The value of this.responseText
is apparently undefined.

A slight variation reveals an apparent explanation:

xhr.onreadystatechange = function () { alert(this); }
resultfunction () { alert(this); }
xhr.open("GET","http://www.squarefree.com/shell/shell.html",true)

This produces an alert that ... displays the code of the anonymous
function itself.

So, apparently, in this case, the "this" keyword refers to the function
itself, and it would seem I can't graft-on methods that have access to
the other properties of the XMLHttpRequest object. At least via the
"this" keyword.

Can someone help me understand what the differences between the two
situations are?

Also... the reason why I'm interested in this largely has to do with
some vague stylistic discomfort. Much of the code I've seen and written
using XMLHttpRequests uses global / lexically scoped variables to get
information across curly-braces between functions. If I could graft
methods onto the request objects themselves, I think I could cut down
on this. I'm open to other suggestions.

Sep 22 '06 #1
1 2243
weston wrote:
So, the following apparently works as a method of grafting a method
onto an object (using the squarefree JS shell):

obj = { x: 1, inc: null }
result [object Object]
obj.inc = function () { this.x++ }
result function () { this.x++; }
obj.inc()
obj.x
result 2

And by works, I mean that when I create an object, and then
assign one of its properties an anonymous function, the
"this" keyword within said anyonymous function apparently
refers to the object itself. Convenient. I like it.
As expressed, that effect is an illusion as - this - is determined
always, and only, by how a function is called. If you call one as a
method of an object, as you do above, - this - will refer to that
object.
The problem is, in the context of an XMLHttpRequest object,
this doesn't seem to work as well for me. Again, using the
squarefree js shell:

xhr = new XMLHttpRequest()
result[object XMLHttpRequest]
xhr.onreadystatechange = function () { alert(this.responseText); }
resultfunction () { alert(this.responseText); }
xhr.open("GET","http://www.squarefree.com/shell/shell.html",true)

Now, one might think this would produce an alert containing
the text/html of shell.html. But it doesn't. The value of
this.responseText is apparently undefined.
The problem is that you don't know how the onreadystatechange handler is
being called by the browser so you cannot know in advance what - this -
will refer to. Experimentation reveals that (for IE at least) it is not
called as a method or any particular object.
A slight variation reveals an apparent explanation:

xhr.onreadystatechange = function () { alert(this); }
resultfunction () { alert(this); }
xhr.open("GET","http://www.squarefree.com/shell/shell.html",true)

This produces an alert that ... displays the code of the
anonymous function itself.
I bet that is not universally true.
So, apparently, in this case, the "this" keyword refers
to the function itself, and it would seem I can't
graft-on methods that have access to the other
properties of the XMLHttpRequest object. At least
via the "this" keyword.
Yes, you cannot use - this - practically, so you must provide access to
objects of interest by some other means (and there are plenty of
options).
Can someone help me understand what the differences
between the two situations are?
The difference is between knowing how a function is being called and not
knowing. Generally when you don't know you can work it out by trying,
but if the results are not useful you still have to deal with that.
Also... the reason why I'm interested in this largely
has to do with some vague stylistic discomfort. Much of
the code I've seen and written using XMLHttpRequests
uses global / lexically scoped variables to get information
Globals are not so good, as multiple asynchronous requests will tend to
overwrite them. Exploiting closures (the lexically scoped stuff) is much
better.
across curly-braces between functions.
If I could graft methods onto the request objects
themselves, I think I could cut down on this. I'm
open to other suggestions.
That is quite an 'if' as there is no reason to expect that the request
object can have properties added to it at runtime (except that that is
common in javascript environments) as they are host objects and may not
provide the facility.

Bus since you cannot get - this - to reliably refer to the request
object the question is academic.

Richard.
Sep 22 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sams | last post by:
How can I assign a return value of a CGI to a JavaScript variable? Or what is the correct way? <script> var myData = eval(http://somewhere/cgi-bin/data.cgi"); </script> TIA Sam
1
by: Jacob Friis Larsen | last post by:
Anyone know how to let processReqChange know that it is in an object? In Php I can do it like this: array($this, 'function_name'); Any idea for a workaround? /Jacob function ajax () { ...
7
by: adam | last post by:
i'm working on a portion of a CMS that allows content-admins to browse a product list, and add individual products into the taxonomy by clicking checkboxes next to categories they might belong in....
1
by: akhil.patel | last post by:
I realize that this may have been asked before, but I could not find a definitive answer. function loadXMLDoc(url, MyFunction) { // use native XMLHttpRequest object if (window.XMLHttpRequest)...
2
by: jhullu | last post by:
hello, I'm writing a program using XMLHttpRequest that works in the main case on IE and mozilla but this code works only on IE ... why ? probleme is located at mark 'HERE' at the end of code....
2
by: petermichaux | last post by:
Hi, I thought it is about time I tried writing some JavaScript with XMLHttpRequest instead of just using the Yahoo! UI library. The simple page below works in both Safari and Opera but I don't...
10
by: HugeBob | last post by:
Hi All, I'm having a problem with a web app I'm writing. I have a form for which I'm going to validate one of the fields. I use AJAX to retrieve XML containing a list of valid entries. My app...
3
pbmods
by: pbmods | last post by:
AN INTRODUCTION TO FUNCTION OBJECTS LEVEL: INTERMEDIATE PREREQS: OBJECTS You've seen it before. You're setting up an XMLHttpRequest call, and you need to execute a function when it returns, so...
5
by: Heofz | last post by:
Hey all, I've been banging my head against a brick wall on this one for the last 12 hours, and the time has come for me to give up and consult the gurus on this one. The below URL contains a...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.