javascript-express
javascript-express copied to clipboard
7.2 Custom Emitter's removeListener method is wrong
The removeLIstener method in the custom Emitter won't filter out the target listener.
removeListener(eventName, listener) { const listeners = this.listeners[eventName] || [] listeners.filter(element => element === listener) this.listeners[eventName] = listeners }
In fact, the second line in the method listeners.filter(element => element === listener)
won't make real modification to the listerners variable, and the original listeners will be returned.
Suggested modification as follow:
removeListener(eventName, listener) { const listeners = this.listeners[eventName] || [] this.listeners[eventName] = listeners.filter(element => element !== listener) }