DEV Community

Cover image for ✋ Avoid EventEmitter Inheritance
Horacio Rivero
Horacio Rivero

Posted on • Updated on

✋ Avoid EventEmitter Inheritance

For a long time I have been observing how a large number of libraries, modules, frameworks, etc. made for Node.js solve a need that arises as follows:

They see the need to add the behavior of the EventEmitter class and what they do is make a complete inheritance of this class 💩, which generates on the one hand, that a class as simple as Person, which can be seen in the image From below, grow unnecessarily in complexity, inheriting all EventEmitter behavior. Now what relationship exists between a Person and the rawListeners method?,
they don't have real neither logical relationship.

It is evident that a person generates events and that he can possess this behavior, but the prototype of a person logically is not EventEmitter, surely many will realize how to solve this problem, applying composition over inheritance.

Here is an example in Express.js, an object called app is created that inherits all EventEmitter behavior:

Express.js example

Now if we see it in this image that I did, it is clearer what I want to express, someone who has to analyze the behavior of a class or quickly visualize the methods and properties, he will meet this context:

Alt Text

Top comments (0)