class MyElement {
constructor () {
this.listeners = {}
setTimeout(() => this.trigger('loaded'), 3000)
}
get (event) {
return this.listeners[event] || (this.listeners[event] = new Set())
}
on (event, listener) {
this.get(event).add(listener)
}
off (event, listener) {
this.get(event).delete(listener)
}
trigger (event, ...args) {
this.get(event).forEach(listener => listener(...args))
}
}
const element = new MyElement()
const listener = () => console.log('it is loaded')
element.on('loaded', listener)