V
V
vetsmen2019-08-20 16:41:23
JavaScript
vetsmen, 2019-08-20 16:41:23

Inheritance and EventEmitter?

There is a class that emits events:
class A extends EventEmitter {}
There is also a second class:
class B
I need to listen to the events that the first class A pushes in it. What is the best way to do this?
If inheritance: class B extends A, then how can I listen to these events, and most importantly where?
The first thing that comes to mind is to create an instance of class A in the construct of class B and listen to events in it, that is:

class B {cunstructor() { this.a = new A(); this.a.on('event', () => {...}) }}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Shvets, 2019-08-20
@vetsmen

Of course, you can inherit and do it this.on('event')in the B constructor, but this will be some kind of nonsense, an object that listens to itself. Why do this if you can call the method right away.
And an object that listens to one of its properties is quite normal, do as you wrote.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question