diff --git a/lib/events.js b/lib/events.js index 1fff1e3616..69e29eaf55 100644 --- a/lib/events.js +++ b/lib/events.js @@ -203,8 +203,15 @@ EventEmitter.prototype.removeAllListeners = function(type) { return this; } - // does not use listeners(), so no side effect of creating _events[type] - if (type && this._events && this._events[type]) this._events[type] = null; + var events = this._events && this._events[type]; + if (!events) return this; + + if (isArray(events)) { + events.splice(0); + } else { + this._events[type] = null; + } + return this; }; diff --git a/test/simple/test-event-emitter-remove-all-listeners.js b/test/simple/test-event-emitter-remove-all-listeners.js index f5676f42b5..39e6468c97 100644 --- a/test/simple/test-event-emitter-remove-all-listeners.js +++ b/test/simple/test-event-emitter-remove-all-listeners.js @@ -29,10 +29,14 @@ function listener() {} var e1 = new events.EventEmitter(); e1.on('foo', listener); e1.on('bar', listener); +var fooListeners = e1.listeners('foo'); +var barListeners = e1.listeners('bar'); e1.removeAllListeners('foo'); -assert.deepEqual([], e1.listeners('foo')); -assert.deepEqual([listener], e1.listeners('bar')); - +assert.deepEqual(e1.listeners('foo'), []); +assert.deepEqual(e1.listeners('bar'), [listener]); +// identity check, the array should not change +assert.equal(e1.listeners('foo'), fooListeners); +assert.equal(e1.listeners('bar'), barListeners); var e2 = new events.EventEmitter(); e2.on('foo', listener);