Last active
December 27, 2015 06:39
-
-
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".
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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