Re: Problem with object instance....




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

.



Relevant Pages

  • Re: Var in PHP 5 question
    ... > I've written a large system for PHP4 that declares class variables ... The var keyword is supported in PHP5, ...
    (comp.lang.php)
  • Re: use of var
    ... Control control = new TextBox; ... var control = new TextBox; ... The latter example demonstrates a marginal improvement when _not_ using "var" in terms of comprehending how the variable will be used, but I feel that difference represents maybe 5% of what there is to be understood, with 95% of the understanding the same either way. ... There are zero code execution performance benefits. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Events and "this" caveats
    ... The fundamental thing to remember about a function's this keyword is ... Mike Winter 1: ... use call or apply to set the this keyword, or pass a reference to the ... var button = this.button; ...
    (comp.lang.javascript)
  • Re: Checking default arguments
    ... def determine_supplied_args: ... # if passed as keyword argument, ... if var in kwds: ...
    (comp.lang.python)
  • RE: Child grids wont show new or edited values unless grid clicked
    ... I would like to confirm my understanding of your issue. ... I understand that the DataGrid which shows keyword and ... SearchPhrase don't get refreshed when data in the DataSet is modified. ...
    (microsoft.public.dotnet.framework.adonet)