9. Event-Driven Programming and Built-In Modules
Activity 13: Building an Event-Driven Module
Solution:
Perform the following steps to complete this activity:
- Import the
eventsmodule:const EventEmitter = require('events'); - Create the
SmokeDetectorclass that extendsEventEmitterand setbatteryLevelto10:class SmokeDetector extends EventEmitter { constructor() { super(); this.batteryLevel = 10; } }In our constructor, because we are extending the
EventEmitterclass and we are assigning a custom property,batteryLevel, we will need to callsuperinside the constructor and setbatteryLevelto10. - Create a
testmethod inside theSmokeDetectorclass that will test the battery level and emit alow batterymessage in the event that the battery is low:test() { if (this...