12

Possible Duplicate:
Removing event listener which was added with bind

I need to dynamically add and remove event listeners. I also need to set the this.

Will this change the function reference?

element.addEventListener('click', funcA);
newFunc = funcA.bind(this);
element.removeEventListner('click', newFunc);

Will removeEventListener know that I want to remove funcA?

Or does it think I'm removing a new function all together?

Community
  • 1
  • 1

1 Answers1

11

Or does it think I'm removing a new function all together?

Yes, .bind() returns a new function object.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • enter is a method in an object literal, and I always want `this` to point to the containing object. –  Jan 19 '13 at 19:22
  • 2
    Yes, you could do `this.enter = (function(){…}).bind(this)`, so that `this.enter` will reference the bound function only – Bergi Jan 19 '13 at 19:23