Revealing Constructor
Example - Read-only event emitter
const EventEmitter = require('events');
class ReadOnlyEmitter extends EventEmitter {
constructor (executor) {
super();
const emit = this.emit.bind(this);
this.emit = undefined; // hide emit function
executor(emit);
}
};
const ticker = new ReadOnlyEmitter((emit) => {
let tickCount = 0;
setInterval(() => emit('tick', tickCount++), 1000);
});
ticker.on('tick', (count) => console.log('Tick', count));Last updated