1 var o = { 2 createXhr : function(){ 3 try { 4 console.log("xx"); 5 return new XMLHttpRequest(); 6 }catch(e){} 7 try{ 8 return new ActiveXObject("Microsoft.XMLHTTP"); 9 }catch(e){} 10 } 11 }; 12 o.createXhr();// xx 13 o.createXhr();// xx 14 o.createXhr();// xx
1 var O = { 2 createXhr : (function(){ 3 try { 4 console.log("yy"); 5 return new XMLHttpRequest(); 6 }catch(e){} 7 try{ 8 return new ActiveXObject("Microsoft.XMLHTTP"); 9 }catch(e){} 10 })(), 11 12 name : "NIck" 13 }; 14 O.createXhr; 15 O.createXhr; 16 O.createXhr; 17 18 var p = { 19 createXhr : (function(){ 20 var f; 21 if(window.XMLHttpRequest){ 22 f = new XMLHttpRequest(); 23 }else{ 24 f = new ActiveXObject("Microsoft.XMLHTTP"); 25 } 26 return f; 27 })() 28 }; 29 30 console.log(p.createXhr);// XMLHttpRequest