messageLogged
const EventEmitter = require('events');
const emitter = new EventEmitter();
// Register a listener
emitter.on('messageLogged', function() {
console.log('Listener called');
});
// Raise an event
emitter.emit('messageLogged');
messageLogged
and passes parametersconst EventEmitter = require('events');
const emitter = new EventEmitter();
// Register a listener
emitter.on('messageLogged', function(arg1, arg2) {
console.log('Listener called', arg2, arg1);
});
// Raise an event
emitter.emit('messageLogged', 'arg-first', 'arg-second');
messageLogged
and passes the object as an argumentconst EventEmitter = require('events');
const emitter = new EventEmitter();
// Register a listener
emitter.on('messageLogged', function(obj) {
console.log('Listener called', obj);
});
// Raise an event & passes the object
emitter.emit('messageLogged', {key1: 'Value 1', key2: 'Value 2'});
const EventEmitter = require('events');
class Logger extends EventEmitter {
log(message) {
// Log the message
console.log(message);
// Raise an event
this.emit('messageLogged', { id: 1, url: 'http://' });
}
}
module.exports = Logger;
Content of the index fileconst Logger = require('./logger');
const logger = new Logger();
// Register a listener
logger.on('messageLogged', function(arg) {
console.log('Listener called', arg);
});
logger.log('message');
press ALT + D
Step 2:
type CMD and pressed Enter
When pressing ALT + D , typing in CMD and pressed Enter when on This PC (and Quick Access) via File Explorer, it opens up the command prompt in the C:\WINDOWS\system32 directory
function invert(x) {
if (x === 0) {
return 'input was zero';
} else {
return 1 / x;
}
}
The function will still work using an if statement instead of an if...else statementfunction invert(x) {
if (x === 0) {
return 'input was zero';
}
return 1 / x;
}
function lessThanFive(numbers) {
for (let number of numbers) {
if (number > 4) {
return false;
}
}
return true;
}