Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 830caeb

Browse filesBrowse files
benglMyles Borins
authored andcommitted
doc, test: symbols as event names
* Document that Symbol can used as event names. * Add test for using Symbol as event names PR-URL: #4151 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 27b9b72 commit 830caeb
Copy full SHA for 830caeb

File tree

Expand file treeCollapse file tree

2 files changed

+27
-3
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+27
-3
lines changed
Open diff view settings
Collapse file

‎doc/api/events.markdown‎

Copy file name to clipboardExpand all lines: doc/api/events.markdown
+4-3Lines changed: 4 additions & 3 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ is opened. All objects which emit events are instances of `events.EventEmitter`.
1010
You can access this module by doing: `require("events");`
1111

1212
Typically, event names are represented by a camel-cased string, however,
13-
there aren't any strict restrictions on that, as any string will be accepted.
13+
there aren't any strict restrictions on that, as any valid property key will be
14+
accepted.
1415

1516
Functions can then be attached to objects, to be executed when an event
1617
is emitted. These functions are called _listeners_. Inside a listener
@@ -59,7 +60,7 @@ Returns the number of listeners for a given event.
5960

6061
### Event: 'newListener'
6162

62-
* `event` {String} The event name
63+
* `event` {String|Symbol} The event name
6364
* `listener` {Function} The event handler function
6465

6566
This event is emitted *before* a listener is added. When this event is
@@ -70,7 +71,7 @@ added.
7071

7172
### Event: 'removeListener'
7273

73-
* `event` {String} The event name
74+
* `event` {String|Symbol} The event name
7475
* `listener` {Function} The event handler function
7576

7677
This event is emitted *after* a listener is removed. When this event is
Collapse file
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
const EventEmitter = require('events');
5+
const assert = require('assert');
6+
7+
const ee = new EventEmitter();
8+
const foo = Symbol('foo');
9+
const listener = common.mustCall(function() {});
10+
11+
ee.on(foo, listener);
12+
assert.deepEqual(ee.listeners(foo), [listener]);
13+
14+
ee.emit(foo);
15+
16+
ee.removeAllListeners();
17+
assert.deepEqual(ee.listeners(foo), []);
18+
19+
ee.on(foo, listener);
20+
assert.deepEqual(ee.listeners(foo), [listener]);
21+
22+
ee.removeListener(foo, listener);
23+
assert.deepEqual(ee.listeners(foo), []);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.