1

私はここでうまく混乱しました。私のシナリオは次のとおりです。

function DesignPad() {  
 function EditBar() {  
  ...  
  this.removeHandler = function() {  
    **// how do I call Dragger.removeAsset**  
  }  
 }  
 function Dragger(){  
  ...  
  this.removeAsset = function() {}  
 }  
 this.init = function() {  
  this.editBar = new EditBar();  
  this.dragger = new Dragger();  
 }  
}  

var dp = new DesignPad();  
...

Dragger.RemoveAsset を呼び出せないようです。理由はわかりました。私の質問は、それをどのように呼び出すかです。

私は似たようなものを分離し続けようとしています (例: Dragger / EditBar) が、イベント ハンドラーであらゆる種類のものが混同されているようです。このことに関する提案、良い読み物などはありますか?

ありがとう。

4

4 に答える 4

3

Douglas Crockford の Javascriptが JavaScript の最良の入門書であることがわかりました。特に Yahoo 向けのビデオ: JavaScript プログラミング言語では、JS でオブジェクトがどのように作成および継承されるかを正確に学ぶことができます。

問題の解決策は次のとおりです。

function DesignPad() {  
  var that = this;
 function EditBar() {  
  this.removeHandler = function() {  
    print("RemoveHandler");
    that.dragger.removeAsset();
  }  
 }  
 function Dragger() {  
  this.removeAsset = function() {
    print("RemoveAsset");
  }  
 }  
 this.init = function() {  
  this.editBar = new EditBar();  
  this.dragger = new Dragger();  
 }
}  

var dp = new DesignPad();
dp.init();
dp.editBar.removeHandler();

しかし、他の人が気づいたように、いくつかのことをリファクタリングできます:)。

于 2009-05-12T19:31:30.897 に答える
0

これを試して:

function DesignPad() {  

 function EditBar(s) {  
  super = s;
  this.removeHandler = function() {
    alert('call 1'); 
    super.dragger.removeAsset();  
  }  
 } 


 function Dragger(s){
  super = s;  
  this.removeAsset = function() {
      alert('call 2'); 
    }  
 }  

 this.init = function() {  
  this.editBar = new EditBar(this);  
  this.dragger = new Dragger(this);  
 }  

}  

var dp = new DesignPad(); 
dp.init()
dp.editBar.removeHandler();
alert('end');
于 2009-05-12T19:37:39.797 に答える
0

私には、そのコードをリファクタリングしてシンプルにする必要があるように思えます。

あなたの問題は、ネストされた関数がプライベートであるため、外部からアクセスできないという事実に起因すると思います。

于 2009-05-12T19:00:04.333 に答える
0

Dragger のインスタンスは DesignPad オブジェクトの「プロパティ」ですか? その場合、そのオブジェクトへの参照を removeHandler() メソッドに渡すことができます。

于 2009-05-12T19:04:37.680 に答える