2013年8月23日星期五

JavaScript function inert loaded

 

I recently Advanced JavaScript programming, have much to gain, the next few days to write about the study notes. Before writing a preliminary understanding Ajax essays, which has a function to Create XmlHttpRequest object, browser compatibility reasons, write code, or if the judge through a lot of try, catch statement will lead to the correct code of the function.

 
  
<script type="text/javascript"> 
function createXHR(){
var xhr = null;
try {
// Firefox, Opera 8.0+, Safari,IE7+
xhr = new XMLHttpRequest();
}
catch (e) {
// Internet Explorer
try {
xhr
= new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
try {
xhr
= new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e) {
xhr
= null;
}
}
}
return xhr;
}
</script>
 
 

each call to this function, it must first be capable browser checks, first check whether the browser supports built XMLHyypRequest object, then check if you do not support all versions of ActiveX-based XMLHttpRequest, each call to the function is In this way, in fact, when first executed, if the browser supports a particular XMLHttpRequest object, then the next execution time does not change the nature of this support, while further testing is not necessary, even if only an if statement, the Executive certainly no slower than if we can make the if statement does not have to perform every time, then you can call in case of frequent improve execution speed. The solution is called lazy loading techniques.

 

inert load

 

inert load function performs a branch represents only the first swap with the function performed when, in the course of the first call, the function will be overwritten by another function to be executed in accordance with the appropriate manner, so that any original function call would not go through the Executive branch. createXHR function can be rewritten as such

 
  
function createXHR(){ 
var xhr=null;
if(typeof XMLHttpRequest !='undefined'){
xhr
= new XMLHttpRequest();
createXHR
=function(){
return new XMLHttpRequest();
}
}
else{
try {
xhr
= new ActiveXObject("Msxml2.XMLHTTP");
createXHR
=function(){
return new ActiveXObject("Msxml2.XMLHTTP");
}
}
catch (e) {
try {
xhr
= new ActiveXObject("Microsoft.XMLHTTP");
createXHR
=function(){
return new ActiveXObject("Microsoft.XMLHTTP");
}
}
catch (e) {
createXHR
=function(){
return null;
}
}
}
}
return xhr;
}
 
 

createXHR in this inert loaded when the first execution of each branch will be reassigned to createXHR, overwrite the original function that returns the xhr object, while the second will be executed when the function is called directly after rewriting so you do not need to perform re-examined each branch.

 

Benefits

 

lazy loading function has two main advantages, the first is the obvious efficiency problem, though, when the first execution of the function would mean an assignment executed slower, but subsequent calls to avoid repeat testing because more fast; second is the appropriate code to be executed only when the function is invoked before the actual execution, many JavaScript libraries when it is loaded in the browser to perform many different branches to achieve all the things set up, while inert loading function The calculated delay, does not affect the execution time of the original script.

没有评论:

发表评论