2009-11-15 63 views
0

這應該很簡單,但由於我是新手編程,特別是在JavaScript中,我無法弄清楚。初學javascript問題

比方說,我有以下的javascript代碼:

var InjectClientValue = Class.create(); 


InjectClientValue.prototype = { 
    initialize: function(sourceElement, eventElement, updateElement) { 
     this.sourceElement = $(sourceElement); 
     this.element = $(eventElement); 
     this.updateElement =$(updateElement) 
     this.element.observe('click',this.onClick.bindAsEventListener(this)); 
    }, 
    onClick: function(event) { 
     new Ajax.Request(this.element.href+"/"+this.sourceElement.value, { 
      method:'get', 

      onSuccess: function(transport) { 

       //How do I access the instance variable updateElement in InjectClientValue_ 
      //updateElement.update(transport.responseJSON.content); 
      } 
     }); 
     event.stop(); 

    } 
    } 

我需要的是訪問變量updateElement從新的Ajax.Request的的onSuccess在初始化設置好的。我該怎麼做?

回答

1

試試這個:

onClick: function(event) { 
    var thisVariable = this; 

    new Ajax.Request(this.element.href+"/"+this.sourceElement.value, { 
     method:'get', 

     onSuccess: function(transport) { 
      thisVariable.updateElement.update(transport.responseJSON.content); 
     } 
0

既然你已經有了類的實例「InjectClientValue,」你可以用它來引用它的對象,並調用方法。

 
onClick: function(event) { 
    new Ajax.Request(this.element.href+"/"+this.sourceElement.value, { 
     method:'get', 
     onSuccess: function(transport) { 
      InjectClientValue.updateElement.update(transport.responseJSON.content); 
     }