2012-04-13 18 views
2

Ajax.useでは、コールバック関数内でコールバック関数を指します。モジュールパターン内のコールバック関数で 'this'と呼ばれるもの

しかし、コールバック定義の外にあるthisは、Ajaxの特定のインスタンスを参照します。

これは、thisAjax.invoke - >this.use()に置き換えて、別のメンバーメソッドからメンバーメソッドを呼び出す方法です。

コールバック関数の内部でメンバーメソッドを呼び出す必要があります。

私は推測して

this.invoke() 

に置くが、私は」それがコールバック関数であるとして、これは正しいオブジェクトを参照していると思ういけません。私はそれがコールバック関数オブジェクトを指すと思います。

/** 
* Ajax 
*/ 

var Ajax = (function() 
{ 
    var Ajax = function (element) 
    { 
     this.object = this.create(); 
    }; 
    Ajax.prototype.create = function() 
    { 
     var request; 
     try 
     { 
      request = new window.XMLHttpRequest(); 
     } 
     catch(error) 
     { 
      try 
      { 
       request = new window.ActiveXObject("Msxml2.XMLHTTP"); 
      } 
      catch(error) 
      { 
       try 
       { 
        request = new window.ActiveXObject("Microsoft.XMLHTTP"); 
       } 
       catch(error) 
       { 
        request = false; 
       } 
      } 
     } 
     // alert("Object Created Successfully"); 
     return request; 
    }; 

    Ajax.prototype.use = function(param, ajax_func) 
    { 
     alert("Ajax.prototype.use Called"); 
     this.object.open("POST", Global.GATEWAY, true); 
     this.object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     this.object.setRequestHeader("Content-length", param.length); 
     this.object.setRequestHeader("Connection", "close"); 
     this.object.onreadystatechange = function() 
     { 
      if(this.readyState === 4) 
      { 
       if(this.status === 200) 
       { 
        ajax_func(this.responseText); 
        return true; 
       } 
       else 
       { 
        this.invoke(param, ajax_func); 
        return false; 
       } 
      } 
     }; 
     this.object.send(param); 
     return true; 
    }; 

    Ajax.prototype.invoke = function(param, ajax_func) 
    { 
     var state = false, 
      count = 1; 
     while(state === false && count <= 5) 
     { 
      if(count !== 1) 
      { 
       alert('Ajax Object Use Failed | Try Again '); 
      } 
      state = this.use(param, ajax_func); 
      count++; 
     } 
     return state; 
    }; 

    return Ajax; 

}()); 

答えて

2

あなたは、コールバックの内側にthisはもはやあなたのAjaxオブジェクトへの参照であることを正確に正しいです。私はvar self = this;を定義し、コールバックの内側にselfではなくthisを使用しています

Ajax.prototype.use = function(param, ajax_func) 
{ 
    alert("Ajax.prototype.use Called"); 
    var self = this; 
    this.object.open("POST", Global.GATEWAY, true); 
    this.object.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
    this.object.setRequestHeader("Content-length", param.length); 
    this.object.setRequestHeader("Connection", "close"); 
    this.object.onreadystatechange = function() 
    { 
     if(this.readyState === 4) 
     { 
      if(this.status === 200) 
      { 
       ajax_func(this.responseText); 
       return true; 
      } 
      else 
      { 
       self.invoke(param, ajax_func); 
       return false; 
      } 
     } 
    }; 
    this.object.send(param); 
    return true; 
}; 

注意:あなたは、コールバック内で使用することができ、それへの参照を作成する必要があります。

+0

「自己」は魔法ではありません。私はself = thisをコールバックの外側に定義しました。 – Prestaul

+1

http://www.javascriptkata.com/2007/05/14/how-to-use-the-self-with-object-oriented-javascript-and-closures/ –

+0

申し訳ありませんが...私はそれを逃した...私はそれがPHPのようなキーワードだと思った... –

関連する問題