From 31c0e79dae292d382cc2566d3954d63c0db4286d Mon Sep 17 00:00:00 2001 From: Yosuke Furukawa Date: Thu, 22 Jan 2015 00:40:01 +0900 Subject: [PATCH 1/2] events: Add EventEmitter.prototype.off for removeListener alias --- lib/events.js | 2 ++ test/parallel/test-event-emitter-method-names.js | 2 +- test/parallel/test-event-emitter-remove-listeners.js | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index 3bc532e06f4a7d..fb5149b0ee923f 100644 --- a/lib/events.js +++ b/lib/events.js @@ -238,6 +238,8 @@ EventEmitter.prototype.removeListener = return this; }; +EventEmitter.prototype.off = EventEmitter.prototype.removeListener; + EventEmitter.prototype.removeAllListeners = function removeAllListeners(type) { var key, listeners; diff --git a/test/parallel/test-event-emitter-method-names.js b/test/parallel/test-event-emitter-method-names.js index 27061df4e4a66d..4a738341aafd03 100644 --- a/test/parallel/test-event-emitter-method-names.js +++ b/test/parallel/test-event-emitter-method-names.js @@ -6,7 +6,7 @@ var E = events.EventEmitter.prototype; assert.equal(E.constructor.name, 'EventEmitter'); assert.equal(E.on, E.addListener); // Same method. Object.getOwnPropertyNames(E).forEach(function(name) { - if (name === 'constructor' || name === 'on') return; + if (name === 'constructor' || name === 'on' || name === 'off') return; if (typeof E[name] !== 'function') return; assert.equal(E[name].name, name); }); diff --git a/test/parallel/test-event-emitter-remove-listeners.js b/test/parallel/test-event-emitter-remove-listeners.js index fd699662cae052..017c3de2fa001d 100644 --- a/test/parallel/test-event-emitter-remove-listeners.js +++ b/test/parallel/test-event-emitter-remove-listeners.js @@ -61,3 +61,6 @@ e4.on('removeListener', common.mustCall(function(name, cb) { e4.on('quux', remove1); e4.on('quux', remove2); e4.removeListener('quux', remove1); + +var e5 = new events.EventEmitter(); +assert.equal(e5.removeListener, e5.off); From d097d84eb49d788cbb2e981f03e453e3fe36c153 Mon Sep 17 00:00:00 2001 From: Yosuke Furukawa Date: Thu, 22 Jan 2015 00:41:06 +0900 Subject: [PATCH 2/2] doc: Add EventEmitter.prototype.off --- doc/api/events.markdown | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/api/events.markdown b/doc/api/events.markdown index 3492619da77e95..ddb9a9a27e7c5b 100644 --- a/doc/api/events.markdown +++ b/doc/api/events.markdown @@ -57,6 +57,7 @@ it is removed. Returns emitter, so calls can be chained. ### emitter.removeListener(event, listener) +### emitter.off(event, listener) Remove a listener from the listener array for the specified event. **Caution**: changes array indices in the listener array behind the listener.