[Javascript] XMLHttpRequestオブジェクトを取得する関数を一工夫加えてみた

どーでもいいことだけど、Ajax関連のブログとかいろいろ見てると下記のように、try/catchのネストしたコードを良く見かけるけど見た感じがスッキリしない。

var createXMLHttpRequest = function(){
  if(window.XMLHttpRequest){
    return new XMLHttpRequest();
  }else if (window.ActiveXObject) {  // IE
    try{
      return new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
      try{
        return new ActiveXObject("Microsoft.XMLHTTP");
      }catch(e){
        throw 'not support.';
      }
    }
  } 
};


関数だしreturnで返すだけなんだからコレでよくね?見やすいし。

var createXMLHttpRequest = function(){
  if(window.XMLHttpRequest) return new XMLHttpRequest();
  if (window.ActiveXObject) {  // IE
    try{ return new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){}
    try{ return new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){}
  }
  throw 'not support.';
};