[thelist] 'classes' in javascript
liorean
liorean at gmail.com
Wed Jun 15 01:12:42 CDT 2005
> The first time I RemoteConnection().request(...) is called first, and
> the alert reports that 'this' is an object. This is what I expect. The
> second method fired is .handle(); this is actually being fired by an
> event trigger that is set up in request():
>
> req.onreadystatechange = this.handle;
>
> The second alert (in handle()) alerts the contents of the function
> (i.e., literally the function's code). I seemingly do not have access
> to the properties of teh containing 'class' (RemoteConnection).
No way to know for sure without seeing more code, but this sounds to
me like the old method-is-just-another-first-class-function problem or
JavaScript: the "this" keyword is bound *when called*, not *when
referenced*. Meaning that changing
req.onreadystatechange = this.handle;
into
var
self=this;
req.onreadystatechange = function(){self.handle()};
should fix it, or alternatively without adding another local variable:
req.onreadystatechange = (function(obj){return
function(){obj.handle()})(this);
--
David "liorean" Andersson
<uri:http://liorean.web-graphics.com/>
More information about the thelist
mailing list