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...