From d168d01b04cefafcdcb2b88d489d2ed0b7a15154 Mon Sep 17 00:00:00 2001 From: Sakthipriyan Vairamani Date: Sun, 12 Jul 2015 16:10:57 +0000 Subject: [PATCH] doc: properly inheriting from EventEmitter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-By: Michaƫl Zasso --- doc/api/events.markdown | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/doc/api/events.markdown b/doc/api/events.markdown index e7470f37aa4877..fbc04a96239b0f 100644 --- a/doc/api/events.markdown +++ b/doc/api/events.markdown @@ -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);