0

I need to create an EventEmitter class with constructor() and on(eventName, callback) methods. Also have to make Movie a subclass of EventEmitter and use the inherited methods to publish play event when the method is called. I don't know how to create subclasses because I can't find examples using class.

After this I need to create a Logger class with constructor() and log(info) methods.

I need to do this:

terminator.on('play', () => logger.log('play'));
terminator.play(); // output: The 'play' event has been emitted

This is what I have until now:

class Movie{
    constructor(name){
        this.name = name;
    }
    play(){
        //not important I think
    }

class Logger{
    constructor(){ 
        //not sure          
    }

    log(info){
        console.log("The '" + info +"' event has been emitted");
    }
}

class EventEmitter{
    constructor(){
        //not sure
    }       
    on(eventName, callback) {
        //idk
    }
}
devius
  • 2,736
  • 22
  • 26
boludo kid
  • 154
  • 1
  • 12
  • 1
    Subclassing is `class Movie extends EventEmitter`. Look at the source of an event emitter like [nanobus](https://github.com/choojs/nanobus) to see how it’s done. – Ben West May 20 '18 at 22:07
  • 1
    Happy Birthday! I'd be glad to tell about subclassing in ES6 way, with `extends` keyword, but honestly you can find it in the internet on your own, and the most important, it will not give you the understanding of how classes and subclasses actually work in JS. [I'd suggest you a little reading](https://github.com/getify/You-Dont-Know-JS/tree/master/this%20%26%20object%20prototypes), it may take time, but will give you 1000x more power! – pttsky May 20 '18 at 22:10

0 Answers0