Double issue with "this" scope in xmlhttprequest javascript <p>I have two huge issues with <code>this</code> scope in my javascript code. After creating <code>setAJAXGet</code> object, callback function was lost and couldn't be called correctly. So instead of calling <code>this.funct</code> I set <code>ajax.parent = this</code>; and call <code>this.parent.funct</code> - works ok.</p> <pre><code>function setAJAXGet() { this.askServer = function() { var ajax = new XMLHttpRequest(); ajax.parent = this; ajax.contentType = "charset:utf-8"; ajax.onreadystatechange = function() { if (ajax.readyState==4 &amp;&amp; ajax.status==200) { this.parent.funct(ajax.responseText); } } ajax.open( "GET", this.url+'?'+this.vars, true ); ajax.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); if (navigator.onLine) ajax.send( null ); else this.functError(); } this.functError; this.funct; this.vars; this.url; } </code></pre> <p>Things get a little more complicated when I try to call <code>setAJAXGet()</code> from the other object and the callback function is inside that object. Callback function is called correctly, but every other function in the object (from the callback function) gets invisible.</p> <pre><code>function someObject() { this.asyncGet = function() { var get = new setAJAXGet(); //do some config here... get.funct = this.processAsyncData; get.askServer(); } this.someOtherFunction = function() { } this.processAsyncData = function(ajaxText) { // ajaxText is OK this.someOtherFunction(); // this.someOtherFunction is not defined (how so?) } this.asyncGet(); } </code></pre> <p>I can solve this problem by passing object to the <code>processAsyncData</code> by modified <code>setAJAXGet()</code> as argument, but it looks ugly. </p> <pre><code>function someObject() { this.asyncGet = function() { var get = new modifiedSetAJAXGet(); //do config here... get.object = this; // stores 'this' and sends it to callback as argument get.funct = this.processAsyncData; get.askServer(); } this.someOtherFunction = function() { } this.processAsyncData = function(object, ajaxText) { // ajaxText is OK object.someOtherFunction(); // object.someOtherFunction works just fine } this.asyncGet(); } </code></pre> <p>I believe that you know more elegant solution.</p>