Skip to content

Instantly share code, notes, and snippets.

@surma
Last active April 6, 2020 17:49
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save surma/e38333c30335f3b5075b62cfbd9b9ef8 to your computer and use it in GitHub Desktop.
Save surma/e38333c30335f3b5075b62cfbd9b9ef8 to your computer and use it in GitHub Desktop.
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