Skip to content

Instantly share code, notes, and snippets.

@surma surma/eventtarget.js
Last active Oct 29, 2018

Embed
What would you like to do?
function eventTarget(o) {
const {port1} = new MessageChannel();
o.dispatchEvent = port1.dispatchEvent.bind(port1);
o.addEventListener = port1.addEventListener.bind(port1);
}
// Usage:
let myObj = { /* ... */ };
eventTarget(myObj);
myObj.addEventListener('custom-event', event => console.log(event));
myObj.dispatchEvent(
new CustomEvent('custom-event', {detail: 'some important detail'})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.