Skip to content

Instantly share code, notes, and snippets.

@eviltik
Last active February 2, 2021 09:03
Show Gist options
  • Save eviltik/69166b77008002cd8736d208d5490afe to your computer and use it in GitHub Desktop.
Save eviltik/69166b77008002cd8736d208d5490afe to your computer and use it in GitHub Desktop.
nodejs simple & stupid "class" using function style and event emitter
const util = require('util');
const EventEmitter = require('events').EventEmitter;
function Fn(opts) {
function start() {
this.emit('start', opts);
}
// export public functions
this.start = start;
return this;
}
util.inherits(Fn, EventEmitter);
module.exports = Fn;
const Fn = require('./fn');
const fn = new Fn({ foo:'bar' });
// or
//const fn = new (require('./fn'))({ foo:'bar' });
fn.on('start', (data) => {
console.log('started, foo=%s', data.foo);
});
fn.start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment