Skip to content

Instantly share code, notes, and snippets.

@hakimel
Last active December 27, 2015 06:39
Show Gist options
  • Save hakimel/7282821 to your computer and use it in GitHub Desktop.
Save hakimel/7282821 to your computer and use it in GitHub Desktop.
Sometimes I wish events worked like this. Normal event listeners: "when X happens do Y" Reversed events listeners: "do Y when X happens".
Function.prototype.on = function( scope, eventName, useCapture ) {
scope.addEventListener( eventName, this, useCapture );
};
Function.prototype.off = function( scope, eventName, useCapture ) {
scope.removeEventListener( eventName, this, useCapture );
};
// Sample listener
function doSomething() { console.log( 'Doing it.' ); }
// Call doSomething when the window resizes
doSomething.on( window, 'reisze' );
// Kill the listener
doSomething.off( window, 'reisze' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment