js惰性思想:
能够执行一次就搞定绝对不会执行第二次
function createXHR(){
var xhr=null,
falg=false,
ary=[
function(){
return new XMLHttpRequest;
},
function(){
return newActiveXObject("Microsoft.XMLHTTP");
},
function(){
return new ActiveXObject("Msxm12.XMLHTTP");
},
function(){
return new ActiveXObject("Msxm13.XMLHTTP");
}
];
var len=ary.length;
for (var i = 0; i<len; i++) {
var curFn=ary[i];
try{
xhr=curFn();
createXHR=curFn;
flag=true;
break;
}catch(e){
//不处理
}
}
if(!flag){
throw new Error('your browser is not support ajax');
}else{
return xhr;
}
}
console.log(createXHR());