如果我这样做
document.addEventListener("mousedown", this.foo);
然后在函数this.foo
内部,然后用
document.removeEventListener("mousedown", this.foo);
然后它有效。
但是,如果我做document.addEventListener(" mousedown",this.foo.bind(this));
然后不删除该功能。
我能做些什么吗?我必须在foo中拥有正确的上下文。
答案 0 :(得分:5)
this.foo.bind(this)
返回的函数与函数this.foo
不同。所以,你需要做的是继续引用bind返回的函数
var handler = this.foo.bind(this);
document.addEventListener("mousedown", handler);
document.removeEventListener("mousedown", handler);