Which is the better way to use events?
A)
var doorBell = new events.EventEmitter()
var light = new events.EventEmitter()
doorBell.emit('open')
light.emit('on')
doorBell.on('open', function(){
console.log('The door is open!');
});
light.on('on', function(){
console.log('The lights are on');
});
B)
var house = new events.EventEmitter()
house.emit('doorOpen')
house.emit('lightOn')
house.on('doorOpen', function(){
console.log('The door is open!');
});
house.on('lightOn', function(){
console.log('The lights are on');
});
I'm wondering if there is any kind of difference between the two, performance wise or any difference really ...
I kinda dislike giving this answer, but I'd say it depends.
As you know, both variants work, so it's just a matter of preference. Objects hide complexity, and events should be used keeping that idea in mind. In your example I think it would be perfectly fine to use one house object that would emit events for turning on the lights, and ringing the doorbell.
As you might foresee, there will be a point at which the addition of events will get messy. So you have to decide for yourself where the boundary lies of whether a certain event belongs to the scope of a certain object.