Skip to content

Instantly share code, notes, and snippets.

@EteimZ
Created February 12, 2023 00:52
Show Gist options
  • Select an option

  • Save EteimZ/413fe613165b2c5648442c4db3955d10 to your computer and use it in GitHub Desktop.

Select an option

Save EteimZ/413fe613165b2c5648442c4db3955d10 to your computer and use it in GitHub Desktop.
Exploring events with a counter class
const EventEmitter = require('node:events');
class Counter extends EventEmitter {
count = 0
increment(){
this.count++;
}
decrement(){
this.count--;
}
print(){
console.log(`Count: ${this.count}`)
}
}
const count = new Counter()
count.on("increment", function(){
this.increment()
})
count.on("decrement", function(){
this.decrement()
})
count.emit("increment")
count.emit("increment")
count.print() // prints 2
count.emit("decrement")
count.print() // prints 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment