2017-05-10 32 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 =>它现在起作用了 –

相关问题