Last active
February 2, 2021 09:03
-
-
Save eviltik/69166b77008002cd8736d208d5490afe to your computer and use it in GitHub Desktop.
nodejs simple & stupid "class" using function style and event emitter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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