Skip to content Skip to sidebar Skip to footer

Node Es6 Class Event Emitter Function?

I'm trying to create a class for which all instances respond to an event: const events = require('events'); const eventEmitter = new events.EventEmitter(); class Camera { cons

Solution 1:

Move it inside the constructor.

const events = require("events");
const eventEmitter = new events.EventEmitter();

class Camera {
    constructor(ip) {
        this.ip = ip;
        eventEmitter.on("recordVideo", this.recordClip.bind(this));
    }

    recordClip() {
        console.log("running record video");
    }
}

Post a Comment for "Node Es6 Class Event Emitter Function?"