Javascript 将对象传递给onreadystatechange

Javascript 将对象传递给onreadystatechange,javascript,ajax,oop,Javascript,Ajax,Oop,我想让AJAX回调在Myfunction中触发警报,并使其正常工作。我如何在ajaxResponse中引用myobject对象 function myobject() { this.val = "worked"; http.onreadystatechange = function ajaxResponse() { if (http.readyState == 4) { this.myFunction(); }

我想让AJAX回调在Myfunction中触发警报,并使其正常工作。我如何在ajaxResponse中引用myobject对象

function myobject() {
    this.val = "worked";

    http.onreadystatechange = function ajaxResponse() {
        if (http.readyState == 4) {
            this.myFunction();
        }

        http.send(null);  
    }

    myobject.prototype.myFunction = function() {
    alert("it "+this.val);
}

只需将父对象附加到xhr对象,如下所示:

http.parent = this; // Append it here
http.onreadystatechange = function ajaxResponse()
{
    if (http.readyState == 4)
        alert(this.parent.value) // Access it here
}
var objTmp = this;
http.onreadystatechange = (function(objTmp){
                         return 
                             function ajaxResponse{alert(objTmp.value)}
                       })(this)

只需将父对象附加到xhr对象,如下所示:

http.parent = this; // Append it here
http.onreadystatechange = function ajaxResponse()
{
    if (http.readyState == 4)
        alert(this.parent.value) // Access it here
}
var objTmp = this;
http.onreadystatechange = (function(objTmp){
                         return 
                             function ajaxResponse{alert(objTmp.value)}
                       })(this)

ajaxResponse是父函数中的一个函数,在javascript中称为闭包。当闭包使用其父函数中定义的变量时,当父函数完成执行时,不能从内存中销毁该变量。 因此,定义一个临时变量,如下所示:

http.parent = this; // Append it here
http.onreadystatechange = function ajaxResponse()
{
    if (http.readyState == 4)
        alert(this.parent.value) // Access it here
}
var objTmp = this;
http.onreadystatechange = (function(objTmp){
                         return 
                             function ajaxResponse{alert(objTmp.value)}
                       })(this)
然后可以在ajaxResponse中使用这个变量objTmp。 或者你可以这样做:

http.parent = this; // Append it here
http.onreadystatechange = function ajaxResponse()
{
    if (http.readyState == 4)
        alert(this.parent.value) // Access it here
}
var objTmp = this;
http.onreadystatechange = (function(objTmp){
                         return 
                             function ajaxResponse{alert(objTmp.value)}
                       })(this)

ajaxResponse是父函数中的一个函数,在javascript中称为闭包。当闭包使用其父函数中定义的变量时,当父函数完成执行时,不能从内存中销毁该变量。 因此,定义一个临时变量,如下所示:

http.parent = this; // Append it here
http.onreadystatechange = function ajaxResponse()
{
    if (http.readyState == 4)
        alert(this.parent.value) // Access it here
}
var objTmp = this;
http.onreadystatechange = (function(objTmp){
                         return 
                             function ajaxResponse{alert(objTmp.value)}
                       })(this)
然后可以在ajaxResponse中使用这个变量objTmp。 或者你可以这样做:

http.parent = this; // Append it here
http.onreadystatechange = function ajaxResponse()
{
    if (http.readyState == 4)
        alert(this.parent.value) // Access it here
}
var objTmp = this;
http.onreadystatechange = (function(objTmp){
                         return 
                             function ajaxResponse{alert(objTmp.value)}
                       })(this)

我的问题是如何在ajaxResponse中调用this.value并引用包含此代码的对象中的值。我的问题是如何在ajaxResponse中调用this.value并引用包含此代码的对象中的值。这将创建一个全局变量,尽管它会破坏对象的点。这将创建一个全局变量变量,但它破坏了对象的点。这仅适用于myobject.prototype.myFunction=函数{alertit+This.val;}如果http.readyState==4{This.myobject.myFunction;}myobject.prototype.myobject={回调:myFunction{alertit+This.val;}它崩溃了。这只对myobject.prototype.myFunction=function{alertit+This.val;}有效,如果http.readyState==4{This.myobject.myFunction;}myobject.prototype.myobject={callback:myFunction{alertit+This.val;}它崩溃了。