event handler scope inside object method

Discussion in 'Javascript' started by trib, Jan 25, 2006.

  1. trib

    trib Guest

    Hi,

    I'm reasonably new to OO javascript, but appreciating it's versatility.
    I'm trying to do an XMLHTTP request from an instance method of a
    object defined by a class, and I would normally embed the response
    handler in the function, like so:

    var req = newXmlHttp();
    req.open("GET", url, true);

    // When the response is received, process it
    req.onreadystatechange = function() {
    if (req.readyState == 4) {
    ..etc...
    }
    }

    But, if req is an instance property, and is referenced as this.req, it
    does not seem to be available within the scope of the inline function.
    As a workaround I'm currently defining the handler function as a
    separate instance method, and setting up the event handler like so:

    var thisobj= this;
    this.req.onreadystatechange = function () { thisobj.processResponse()
    };

    Is there any way to keep it as an inline function?

    Cheers,

    Andrew
    trib, Jan 25, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joel
    Replies:
    1
    Views:
    371
    John Saunders
    Jul 25, 2004
  2. sonic
    Replies:
    1
    Views:
    521
    John Saunders
    Jan 7, 2005
  3. Replies:
    1
    Views:
    682
    Damien
    Feb 22, 2007
  4. =?iso-8859-2?q?Marcin_St=EApnicki?=

    scope, modyfing outside object from inside the method

    =?iso-8859-2?q?Marcin_St=EApnicki?=, Sep 24, 2007, in forum: Python
    Replies:
    5
    Views:
    329
    Gabriel Genellina
    Sep 27, 2007
  5. Joel

    Page object disappears inside event handler

    Joel, Jul 22, 2004, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    101
Loading...

Share This Page