var Try = {
these : function() {
var returnValue;
for(var i=0; i<arguments.length; i++) {
var lambda = arguments[i];
try {
returnValue = lambda();
break;
} catch(e) {}
}
return returnValue;
}
}
//创建XMLHTTPRequest对象
var xmlhttp = Try.these(
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
function() {return new XMLHttpRequest()}
) || false;
最后的"|| false"是为了保证如果创建xmlhttp失败,能够被显示的赋给一个false的值,避免undefined可能造成的代码异常
these : function() {
var returnValue;
for(var i=0; i<arguments.length; i++) {
var lambda = arguments[i];
try {
returnValue = lambda();
break;
} catch(e) {}
}
return returnValue;
}
}
//创建XMLHTTPRequest对象
var xmlhttp = Try.these(
function() {return new ActiveXObject('Msxml2.XMLHTTP')},
function() {return new ActiveXObject('Microsoft.XMLHTTP')},
function() {return new XMLHttpRequest()}
) || false;