Skip to content

Commit d33b40c

Browse files
committed
doc: properly inheriting from EventEmitter
There are so many buggy code out there, just because not inheriting properly from `EventEmitter`. This patch gives a official recommendation.
1 parent 5acad6b commit d33b40c

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

doc/api/events.markdown

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,22 @@ added.
162162
This event is emitted *after* a listener is removed. When this event is
163163
triggered, the listener has been removed from the array of listeners for the
164164
`event`.
165+
166+
### Inheriting from 'EventEmitter'
167+
168+
Inheriting from `EventEmitter` is no different from inheriting from any other
169+
constructor function. For example:
170+
171+
```js
172+
'use strict';
173+
const util = require('util');
174+
const EventEmitter = require('events').EventEmitter;
175+
176+
function MyEventEmitter() {
177+
// Initialize necessary properties from `EventEmitter` in this instance
178+
EventEmitter.call(this);
179+
}
180+
181+
// Inherit functions from `EventEmitter`'s prototype
182+
util.inherits(MyEventEmitter, EventEmitter);
183+
```

0 commit comments

Comments
 (0)