mirror of https://github.com/nodejs/node.git
doc: properly inheriting from EventEmitter
There are so many buggy code out there, just because not inheriting properly from `EventEmitter`. This patch gives an official recommendation. PR-URL: https://github.com/nodejs/io.js/pull/2168 Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Michaël Zasso <mic.besace@gmail.com>pull/2168/merge
parent
500f2538cc
commit
d168d01b04
|
@ -162,3 +162,20 @@ added.
|
|||
This event is emitted *after* a listener is removed. When this event is
|
||||
triggered, the listener has been removed from the array of listeners for the
|
||||
`event`.
|
||||
|
||||
### Inheriting from 'EventEmitter'
|
||||
|
||||
Inheriting from `EventEmitter` is no different from inheriting from any other
|
||||
constructor function. For example:
|
||||
|
||||
'use strict';
|
||||
const util = require('util');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
function MyEventEmitter() {
|
||||
// Initialize necessary properties from `EventEmitter` in this instance
|
||||
EventEmitter.call(this);
|
||||
}
|
||||
|
||||
// Inherit functions from `EventEmitter`'s prototype
|
||||
util.inherits(MyEventEmitter, EventEmitter);
|
||||
|
|
Loading…
Reference in New Issue