Reputation: 105
I am new to nodejs. I came across these two ways as follow for instantiating EventEmitter
class as follows -
const events = require('events');
const e = new events.EventEmitter();
const e1 = new events();
console.log(e)
console.log(e1)
How come both e
and e1
are instantiated from EventEmitter
? Which is the right way? Am I m missing anything here?
Upvotes: 0
Views: 292
Reputation: 21
According to Events module line#68 and Events module line#73
node -p "events.EventEmitter === events"
This will get the result of true, which means "events.EventEmitter" and "events" in your code can be used to instantiate EventEmitter.
Upvotes: 2
Reputation: 2398
First, in your code, you need to define EventEmitter
so that you can create instance new EventEmitter();
const events = require('events');
const EventEmitter = require('events'); // correct in your code
const e = new events.EventEmitter();
const e1 = new EventEmitter();
console.log(e)
console.log(e1)
Which is the right way
they are both the correct way and it is up to you in which way you want to write
I personally prefer
e1
because it looks clean
const events = require('events');
const e = new events.EventEmitter();
const e1 = new events();
console.log(e)
console.log(e1)
e1 is a clean way but Constructor function name should be Captial, convention in Javascript is to only capitalize constructors
const Events = require('events');
const e1 = new Events();
Upvotes: 0