2010-12-03 432 views
1

比方說,我有一個類:JavaScript對象是函數的範圍

var asdf = new Class({ 
    myFunction: function() { 
    //some stuff here 
    }, 
    anotherFunction: function() { 
    globalObject.dosomethingandusecallback( 
     function() { // this is the callback 
     //how do I call myFunction() here? I can't seem to get it to work? 
     } 
    ); 
    } 
}); 

我似乎在試圖我的回調函數的定義中調用myFunction的一些範圍界定問題。我在這裏錯過了什麼?我認爲它應該在這種情況下訪問myFunction?

謝謝!

回答

3

複製this關鍵字輸入到回調函數的變量之外,並且使用回調內部該變量:

anotherFunction: function() { 
    var self = this; 
    globalObject.dosomethingandusecallback( 
    function() { // this is the callback 
     self.myFunction(); 
    } 
); 
}