我有这两种方法:
Cluster.prototype.initiate_xhr_request = function(url, callback) {
var self = this,
request = (Modernizr.test_xmlhttprequest) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
request.onreadystatechange = function() {
if(request.readyState === 4) {
switch(request.status) {
case 200:
callback.apply(request,[ request.statusText ]);
break;
case 400:
callback.apply(request,[ request.statusText ]);
break;
default:
break;
};
} else
console.log('An error occured during the XHR request');
}
};
request.open("HEAD", url, false);
request.send();
};
Cluster.prototype.operations = {
'&&': function(array){
return array.reduce(function(previousValue, currentValue, index, array){
return previousValue && currentValue;
})
},
'||' : function(array){
return array.reduce(function(previousValue, currentValue, index, array){
return this.initiate_xhr_request(previousValue) || currentValue;
})}
};
编辑:调用方法如下:
Cluster.prototype.calculate = function(operation, array){
return this.operations[operation](array);
};
/* call the calculate function */
this.calculate('&&', some_array);
显然,这条线this.initiate_xhr_request(previousValue)
行不通。我一直试图找到一个很好的解决方案来做我想做的事情,但我找不到一个(:有没有办法做到这一点并保持我的结构不变?