The Node Event Emitter - Part 2

Events.js

Big word

Magic String: A string that has some special meaning in our code.

This is bad because it makes it easy for a typo to cause a bug, and hard for tools to help us find it.

The code

var Emitter = require('events');
var eventConfig = require('./config').events;

var emtr = new Emitter();

emtr.on(eventConfig.GREET, function() {
    console.log('Somewhere, someone said hello.');
});

emtr.on(eventConfig.GREET, function() {
    console.log('A greeting occurred!');
});

console.log('Hello!');
emtr.emit(eventConfig.GREET);
  • Instead, use a configure file to avoid typo
module.exports = {
    events: {
        GREET: 'greet'
    }
}

results matching ""

    No results matching ""