0

次のような JavaScript クラスがあるとします。

var Person = {
    //Private members

    getXmlRequestObj : function() {
            if (window.XMLHttpRequest) {
                return new XMLHttpRequest();
            } else if(window.ActiveXObject) {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } else {
                return "Your Browser needs an upgrade";
            }
        },

initialize: function() {
        Person.importHTML = Person.getXmlRequestObj();
    },


    //Set up the global content retrieval object
    //called importHTML
    //importHTML:  new ActiveXObject("Microsoft.XMLHTTP"),

    _status:'',

    getAjaxList:function(sWhat,sUrl) {
        if (Person.importHTML.readyState == 4 || Person.importHTML.readyState == 0) {
            Person.importHTML.open("GET", sUrl, false);// make it wait for the response
            Person._sWhat = sWhat;
            Person.importHTML.onreadystatechange = Person.handleListArrived; 
            Person.importHTML.send(null);
        }       
    },
     handleListArrived: function() {
        if (Person.importHTML.readyState == 4) {
            if(Person._sWhat=="units")
                Person._unitList = Person.importHTML.responseText;
            if(Person._sWhat=="ranks")
                Person._rankList = Person.importHTML.responseText;
        }
    }
}

このコードを使用する代わりに:

            if(Person._sWhat=="units")
                Person._unitList = Person.importHTML.responseText;
            if(Person._sWhat=="ranks")
                Person._rankList = Person.importHTML.responseText;

Person._sWhat が Person の新しいメンバーになるものは何でも使用したい。つまり、Person の新しいメンバーを動的に割り当てるにはどうすればよいでしょうか。これにより、コードは次のようになります。

Person._sWhat = Person.importHTML.responseText; の効果に対する Person.something。

4

2 に答える 2

0
getAjaxList:function(sWhat,sUrl) {
    if (Person.importHTML.readyState == 4 || Person.importHTML.readyState == 0) {
        Person.importHTML.open("GET", sUrl, false);// make it wait for the response
        Person.importHTML.onreadystatechange = Person.handleListArrived.bind(Person.importHTML, sWhat); //bind is supported in IE9+
        Person.importHTML.send(null);
    }       
},
handleListArrived: function(sWhat) {
    if (this.readyState == 4) {
        Person[sWhat] = this.responseText;
    }
}

または、以下は ecmascript 1.3 以降でサポートされています。

getAjaxList:function(sWhat,sUrl) {
    if (Person.importHTML.readyState == 4 || Person.importHTML.readyState == 0) {
        Person.importHTML.open("GET", sUrl, false);// make it wait for the response
        Person.importHTML.onreadystatechange = function(){ 
            Person.handleListArrived.call(Person.importHTML, sWhat); 
        };
        Person.importHTML.send(null);
    }       
}
于 2013-01-18T19:11:13.507 に答える
0

The dot notation is a convenience in JavaScript that is equivalent to square bracket notation:

Foo.xyz === Foo['xyz']

So, in your case

Person[Person._sWhat+"List"] = Person.importHTML.responseText

would be sufficient, but for the trailing "s" in your _sWhat field. If you can remove that elsewhere, that would be ideal. Alternatively:

Person[Person._sWhat.slice(0,-1)+"List"] = Person.importHTML.responseText
于 2013-01-18T19:11:44.870 に答える