Created
January 18, 2017 11:02
-
-
Save laphilosophia/debcd794a5eec8905e7f088633a2e493 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var evts = (function () { | |
'use strict'; | |
var listeners = [], | |
add, | |
remove; | |
add = function (evt, elem, fn) { | |
var callback = function (evt) { | |
fn(evt.target); | |
}; | |
listeners.push({ | |
evt: evt, | |
elem: elem, | |
fn: fn, | |
callback: callback, | |
removed: false | |
}); | |
elem.addEventListener(evt, callback); | |
}; | |
remove = function (evt, elem, fn) { | |
var callback; | |
each(listeners, function (evtObj) { | |
if (evtObj.evt === evt && evtObj.elem === elem && evtObj.fn === fn && !evtObj.removed) { | |
evtObj.removed = true; | |
callback = evtObj.callback; | |
return false; | |
} | |
}); | |
elem.removeEventListener(evt, callback); | |
}; | |
return { | |
add: add, | |
remove: remove | |
}; | |
}()); | |
var callback = function (evt, target) { | |
console.log(target); | |
}; | |
evts.add('click', document, callback); | |
evts.remove('click', document, callback); | |
var root = document.getElementByTagName('element'); | |
evts.add('click', root, function(message){ | |
root.innerHTML = message; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment