Some memories in JS only need to be executed once. For example, browser type detection is the most commonly used function, because when we use Ajax, we need to detect the browser's built-in XHR. We can record the type during the first detection, and there is no need to detect the browser type when using Ajax in the future. In JS, even if there is only one if statement, it is always more efficient than no if statement.
Normal Ajax method
/**
* JS lazy function
*/
function ajax(){
If(typeof XMLHttpRequest != "undefined"){
return new XMLHttpRequest();
}else if(typeof ActiveXObject != "undefined"){
If(typeof arguments.callee.activeXString != "string"){
var versions = ["MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0", "MSXML2.XMLHttp"];
for(var i=0,k=version.length;i
try{
new ActiveXObject(versions[i]);
arguments.callee.activeXString = versions[i];
break;
}catch(ex){
throw ex;
}
}
return new ActiveXObject(arguments.callee.activeXString);
}else{
throw "No XHR object";
}
}
Every time the ajax() function is called, the browser's built-in XHR must be checked, which is not efficient.
Use the lazy approach
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn