2016-02-01 60 views
0

如果我做如何在回調removeEventListener與.bind(本)附

document.addEventListener("mousedown", this.foo); 

,然後內部功能this.foo然後用

document.removeEventListener("mousedown", this.foo); 

然後它刪除。但是,如果我做document.addEventListener(「mousedown」,this.foo.bind(this));

然後該功能不會被刪除。

有什麼我可以做的嗎?我必須在foo內部有正確的上下文。

+1

您必須存儲對'this.foo.bind(this)'返回的函數的引用。 – Oriol

回答

3

this.foo.bind(this)返回的功能不同於功能this.foo。所以,你需要做的是繼續引用綁定返回的函數

var handler = this.foo.bind(this); 

document.addEventListener("mousedown", handler); 

document.removeEventListener("mousedown", handler); 
相關問題