From d33b40ce940f993247a43bdb20360bb8756ca6f0 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Sun, 12 Jul 2015 16:10:57 +0000 Subject: [PATCH 1/2] 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. --- doc/api/events.markdown | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/doc/api/events.markdown b/doc/api/events.markdown index e7470f37aa4877..bb5a3ff73668a7 100644 --- a/doc/api/events.markdown +++ b/doc/api/events.markdown @@ -162,3 +162,22 @@ 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: + +```js +'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); +``` From 3814d538bc214d506aa6fd6e7447f5d7ca90c707 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Wed, 29 Jul 2015 13:56:48 +0530 Subject: [PATCH 2/2] Fixing the formatting --- doc/api/events.markdown | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/doc/api/events.markdown b/doc/api/events.markdown index bb5a3ff73668a7..fbc04a96239b0f 100644 --- a/doc/api/events.markdown +++ b/doc/api/events.markdown @@ -168,16 +168,14 @@ triggered, the listener has been removed from the array of listeners for the Inheriting from `EventEmitter` is no different from inheriting from any other constructor function. For example: -```js -'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); -``` + '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);