return a variable from a event listener
<p>I have a generic function for XMLHttpRequests. as follows:</p>

<pre><code>function XMLRequest(address, data){
    this.html = "null";
    this.xml = null;
    this.stat = null;
    req = new XMLHttpRequest();

    req.addEventListener('readystatechange', function(this) {

        console.log("from event listener, this.html = "+this.html);
        this.ready = req.readyState;
        if(req.readyState == 4)
        {
            this.stat = req.readyState;
            if(req.status == 200)
            {
                try{
                    this.xml = req.responseXML.documentElement;
                }
                catch(err){
                    this.xml = err;
                }

                try{
                    this.html = req.responseText;
                }catch(err){
                    this.html = err;
                }
                console.log("html = "+this.html);
            }
        }   
    }, false);

    req.open("GET", address+"?"+data, true);
    req.setRequestHeader('content-type', "text/xml");
    req.send();    
    return this;

};
</code></pre>

<p>I made it with the idea that i would use the  <code>watch()</code> function to watch for a change in in <code>this.html</code> or <code>this.xml</code>. However, I never saw the change, and I realized why;</p>

<p>Inside of the anonymous listener function, this refers  to the anonymous function, not <code>XMLRequest</code></p>

<p>I was hoping to find a way around this, to somehow get the xml &amp; text response out of the listener function and into the <code>this.xml</code> &amp; <code>this.html</code> of the <code>XMLRequest</code> function, so that I can then watch for it</p>

<p>Is there any way I can do that?</p>

<p>--edit--</p>

<p>Brad's example, with my edits, for discussion:</p>

<pre><code>function MyObject(){
    this.publicFoo = 'BAR';
    var privateFoo = 'bar';

    var self = this; // store current context so we can reference it in other scopes.
    this.CallMe = function(){
        self.publicFoo = 'newBAR';
        setTimeout(function(){
            alert('public: ' + self.publicFoo + '\nprivate: ' + privateFoo);
        },100);
    };

    return this;
};

var myobj = MyObject();
myobj.CallMe();
alert(myobj.publicFoo)  &lt;-- I want this to alert "newBAR", not "BAR"
</code></pre>
