I see this pattern in quite a few Node.js libraries:
Master.prototype.__proto__ = EventEmitter.prototype;
(source here)
Can someone please explain to me with an example, why this is such a common pattern and when it’s handy?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
As the comment above that code says, it will make
Masterinherit fromEventEmitter.prototype, so you can use instances of that ‘class’ to emit and listen to events.For example you could now do:
Update: as many users pointed out, the ‘standard’ way of doing that in Node would be to use ‘util.inherits’:
2nd Update: with ES6 classes upon us, it is recommended to extend the
EventEmitterclass now:See https://nodejs.org/api/events.html#events_events