0

我对 Javascript 还是很陌生,我一生都无法弄清楚为什么以下对象属性没有传输。

我按如下方式调用对象:

var URL = "TABLE=_Products&COLUMNS=price_Qty,Sale&MATCH=internal_Model&ROWS="+itemnum ;
var ITEM = new get_Database_Info(URL) ;

get_Database_Info 是:

function get_Database_Info(PARAMS) {
    alert(toString(this));
    var URL = document.location.protocol+'//'+document.location.host+'/Catalog/Tools/ajax_Database_Request.php' ;

    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else{// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    if(!xmlhttp){alert('Error: Cannot send XML request.');}
        xmlhttp.onreadystatechange=function() {

            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                alert(toString(this));
                var RESPONSE = xmlhttp.responseText ;
                RESPONSE = RESPONSE.replace(/^\s+/, '');
                var ARR = RESPONSE.split('||') ;
                ARR.pop() ;
                for(var i=0; i<ARR.length; i++){
                    var temparr1 = ARR[i].split('=') ;
                    var NUM = temparr1[0] ;
                    this[NUM] = new Array() ;
                        var temparr2 =   temparr1[1].split('/|') ;
                        temparr2.shift() ;
                            for(var x=0; x<temparr2.length; x++){

                                var temparr3 = temparr2[x].split('??') ;

                                this[NUM][temparr3[0]] = temparr3[1] ;

                            }
                }

            }
        }

    xmlhttp.open("POST", URL, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", PARAMS.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(PARAMS);

}

我已经验证所有属性都在 get_Database_Info 范围内的“this”中,但它们没有转移回 ITEM。

4

2 回答 2

0

我冒昧地猜测,当附加到 onreadystatechange 的函数运行时,this它不再附加到在构造函数中创建的对象,但可能附加到全局对象或xmlhttp对象。我会尝试使用var that=this模式:

function get_Database_Info(PARAMS) {
    alert(toString(this));
    var URL = document.location.protocol+'//'+document.location.host+'/Catalog/Tools/ajax_Database_Request.php' ;

    if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp=new XMLHttpRequest();
        }
    else{// code for IE6, IE5
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

    var that=this; // value of that will be stored in the closure

    if(!xmlhttp){alert('Error: Cannot send XML request.');}
        xmlhttp.onreadystatechange=function() {
            if (xmlhttp.readyState==4 && xmlhttp.status==200){
                alert(toString(that));
                var RESPONSE = xmlhttp.responseText ;
                RESPONSE = RESPONSE.replace(/^\s+/, '');
                var ARR = RESPONSE.split('||') ;
                ARR.pop();
                var arrLength = ARR.length; // always precompute array length
                for(var i=0; i<ARR.length; i++){
                    var temparr1 = ARR[i].split('=') ;
                    var NUM = temparr1[0] ;
                    // that is actually equal to the object that I created
                    // in the constructor.
                    that[NUM] = new Array() ;
                    var temparr2 =   temparr1[1].split('/|') ;
                    temparr2.shift() ;
                    var arrayLength = temparr2.length; // always precompute length
                    for(var x=0; x<arrayLength; x++){
                        var temparr3 = temparr2[x].split('??') ;
                        that[NUM][temparr3[0]] = temparr3[1] ;
                    }
                }
            }
        }

    xmlhttp.open("POST", URL, true);
    xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.setRequestHeader("Content-length", PARAMS.length);
    xmlhttp.setRequestHeader("Connection", "close");
    xmlhttp.send(PARAMS);

此外,如评论中所述,当您在循环中迭代数组中的值时for,不应直接使用 array.length ,因为length每次循环都会调用该方法,从而增加了大量不必要的工作.

此外,正如@Guffa 所指出的,添加值的函数是异步调用的,因此在 XmlHttpRequest 完成之前属性将不存在,这是一种非常危险的反模式。我强烈建议不要这样做。最好让您的构造函数对数据进行同步请求。

于 2011-03-23T17:41:06.183 回答
0

你什么时候检查对象的内容?该请求是异步的,因此您必须等到回调处理完响应之后,对象中才会有任何属性。

If you look for the properties immediately after the object is created, they will never be there. Even if the response is really quick, the first time that the callback can run is when you exit the function where you created the object, so the browser gets the control back.

于 2011-03-23T17:48:36.850 回答