[thelist] 'classes' in javascript

VOLKAN ÖZÇELİK volkan.ozcelik at gmail.com
Wed Jun 15 01:12:12 CDT 2005


> req.onreadystatechange = this.handle;

>    this.handle = function()
>        {
>            alert(this);
> // ...
>        }; //

it is a normal behavior, and computers are always right :) 
"this" keyword in the method, refers to the function's owner
(req.onreadystatechange in this case). which is the this.handle()
function and not the object itself.

Prototype-based programming is somewhat different than strict object
oriented programming (java, c#, etc).

To ge what you want to do you may want to assign a global reference
(which is a no-no in Java but sometimes you need to in OO JavaScript)

Hope the below should help:

var g_RemoteConnection = null;

 function RemoteConnection()
 {
    g_RemoteConnection =  this;
    this.aRequests = new Array();
    this.aRequests[0] = null;
 
    this.request = function(url, method, requestxml)
        {
        alert(g_remoteConnection);
 // ...
        };

...

There may be a more elegant solution: You can create an object
registry and access to the object reference via the methods of it.
However a global reference should suffice in the current case.

For more info on "this" keyword: quirksmode.org is a valuable place,
where the cross-browser differences between the "this" keyword
explained. (btw, quirksmode.org is a "must see" and add several levels
to your js know-how no matter what)
Also you may consider  http://www.codeproject.com/jscript/cbobject.asp
which describes some prototype-based js-coding.


HTH,
Volkan.


More information about the thelist mailing list