This is a follow-up to my previous SO question. In that question, I've defined an index.js
file that includes an on
event handler. I've since updated the index.js
file to look like this:
index.js
const EventEmitter = require('events');
const Student = require('./student');
const Whiteboard = require('./whiteboard');
class Classroom {
constructor() {
this.whiteboard = new Whiteboard();
}
async function start() {
eventEmitter.on('newListener', (event, listener) => {
console.log(`Added ${event} listener.`);
});
eventEmitter.on('handRaised', (question) => {
console.log(question);
this.whiteboard.write(question);
});
for (let i=0; i<10; i++) {
let student = new Student(`Student #${i+1}`);
student.attend();
}
}
}
When I run my app, the line with this.whiteboard.write(question);
generates an error. That error is: Cannot read property 'write' of undefined
. I've confirmed that this.whiteboard
is undefined. My question is, how do I get access to class properties on events?
Thank you!