Skip to content

Instantly share code, notes, and snippets.

@darbicus
Created August 29, 2014 23:00
Show Gist options
  • Save darbicus/46d72aed17f93d61b4d6 to your computer and use it in GitHub Desktop.
Save darbicus/46d72aed17f93d61b4d6 to your computer and use it in GitHub Desktop.
Sometimes I wish addEventListener in javascript would return an object so that you can cancel the event without having to name the function. So I created one but instead of replacing the prototype function I just added a new function addEvent which takes the same arguments as the addEventListener function and it returns a function that removes t…
EventTarget.prototype.addEvent = function () {
var args = [].slice.call(arguments);
var remove = (function () {
this.removeEventListener.apply(this, args);
}).bind(this);
this.addEventListener.apply(this, arguments);
return remove;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment