Re: Problem with object instance....
- From: "Julian Turner" <julian@xxxxxxxxxxxxxx>
- Date: 29 Nov 2005 23:52:30 -0800
Fabio Cavassini wrote:
[snip]
> this.req.onreadystatechange = this.Process; //this.req !=
AFAIK, the problem is here, and your understanding of the "this"
keyword.
Try instead:-
var INSTANCE=this;
this.req.onreadystatechange = function() {INSTANCE.Process();};
In rough terms, the value of "this" within a function depends on what
is calling the function.
If I have:-
myObjInstance.Process()
then "this" is set to "myObjInstance" within the Process function.
However if you just call
Process()
then "this" refers to the window object in the browser. I.e. it is as
if you called:-
window.Process()
In your code, the effect of the assignment expression
this.req.onreadystatechange = this.Process;
Is to pass a reference to the "Process" function alone, which does not
include the "this." context.
Accordingly when "onreadystatechange" is fired, is is effectively
calling "Process()", not "obj.Process()".
So "this" will point to the window object, which does not have a "req"
property.
The solution provided above uses closures.
Read this article on closures:-
<URL:http://jibbering.com/faq/faq_notes/closures.html>
Regards
Julian Turner
.
- References:
- Problem with object instance....
- From: Fabio Cavassini
- Problem with object instance....
- Prev by Date: Re: Problem with object instance....
- Next by Date: Firefox 1.5 released
- Previous by thread: Re: Problem with object instance....
- Next by thread: Re: Signature Capture Applet - Version 2
- Index(es):
Relevant Pages
|