There are a lot of example not using events.EventEmitter.call(this)
in custom event emitter constructors, while other are using it (official documentation):
var events = require('events')
, util = require('util');
var MyEmitter = function () {
events.EventEmitter.call(this);
};
util.inherits(MyEmitter, events.EventEmitter);
MyEmitter.prototype.write = function () {
this.emit('tick');
};
With my basic understandings of JavaScript I don't know if I need it. Is the call necessary to to initialization stuff inside the EventEmitter
?
The EventEmitter. call(this) , when executed during Dog instance creation, appends properties declared from the EventEmitter constructor to Dog . Remember: constructors are still functions, and can still be used as functions. Oh, so it is just the constructer of EventEmitter?
Node. js uses events module to create and handle custom events. The EventEmitter class can be used to create and handle custom events module.
Many objects in a Node emit events, for example, a net. Server emits an event each time a peer connects to it, an fs. readStream emits an event when the file is opened. All objects which emit events are the instances of events.
Yes, it is.
Before Node 0.10, it wouldn't break if you forget that.
Now, it will:
The EventEmitter constructor initializes various properties now. It still works fine as an OOP inheritance parent, but you have to do inheritance properly. The Broken-Style JS inheritance pattern will not work when extending the EventEmitter class. This inheritance style was never supported, but prior to 0.10, it did not actually break.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With