2017-05-10 33 views
1

我不知道如何處理這個用戶交互問題。 API是3DMolJS/TS必須使用綁定,但仍然需要原始的

我試圖同時訪問this元素

model.setClickable({chain: theChain, resi: mappedPosition}, true, function(){ 
    console.log(this); 
}); 

首先,對於原執行如上this將用戶點擊的位置。不過,我還需要將點擊位置與來自外部調用對象的某個值相結合。

我試過.bind(null,this)但功能this設置爲null

我嘗試了封閉

const clickClosure = function(){ 

    const mutations = self.alignment.mutations; 

    function clicker(){ 
     console.log(this); 
     console.log(mutations); 
    } 

    return clicker(); 
} 

model.setClickable({chain: theChain, resi: mappedPosition}, true,clickClosure); 

發現突變存在,但是沒有定義this。任何想法如何獲得這兩個不使用全局變量?

回答

1

使用lambda函數傳遞一個詞法this

model.setClickable({chain: theChain, resi: mappedPosition}, true, (stuff) => { 
    console.log(this, stuff); // no `this` binding to the function 
}); 

那應該保持this勢必給調用者,這是真正得到這樣的回調參考的唯一途徑。

+1

謝謝!我完全忘了lambda notation =>它現在起作用了 –

相關問題