假设我有一个如下所示的 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.something 的效果 Person._sWhat = Person.importHTML.responseText;