Last active
August 29, 2015 13:57
-
-
Save dfkaye/9542550 to your computer and use it in GitHub Desktop.
tiny event on/off p.o.c. ~ for the scrapbook
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
// MOD MAR 6, 2015 | |
function event() { | |
var e = this instanceof event ? this : new event(); | |
e.events = {}; | |
return e; | |
} | |
event.prototype.on = function on(type, listener) { | |
var events = this.events, e = events[type]; | |
!e && (e = events[type] = []); | |
e.push(listener); | |
// return?? | |
}; | |
event.prototype.off = function off(type, listener) { | |
var n, events = this.events, e = events[type]; | |
!e || (n = e.length); | |
while (e && n--) { e[n] === listener && e.splice(n, 1) } | |
// return?? cleanup?? | |
} | |
event.prototype.fire = function fire(data) { | |
// TODO | |
} | |
// quickie test | |
var h = { handleEvent: function (e) { console.log('handled ' + e); } }; | |
var test = event(); | |
test.on('blah', h); | |
console.log(test.events); | |
test.off('blah', h) | |
console.log(test.events); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment