Vic
Vic

Reputation: 723

Does EventEmitter simply map functions in an object?

For example

var event = {
    message: function(message){console.log(message);}
}

//polling here
{
    if(response.type == "message")
    {
        event[response.type](response.message);
    }
}

should work similarly to

events.on("message", function(message){console.log(message);});

//polling here
{
    events.emit(response.type, response.message);
}

If not, how is it different to EventEmitter other than auxiliary functions like .once?

I'm using both at the same time for different reasons. EventEmitter is for event-driven functions while the event object is for keeping an organized set of functions to trigger from a single context (event).

EDIT:

Modified code to work with multiple functions/listeners.

var event = {
    message: []
}

event.message.push(function(message){console.log(message)});

//polling here
{
    if(response.type == "message")
    {
        for(var func in event[response.type])
        {
            event[response.type][func](response.message);
        }

    }
}

Upvotes: 1

Views: 684

Answers (1)

Nivesh
Nivesh

Reputation: 2603

Well, in broader perspective, it is indeed an object(EvenEmitter class) which exposes set of methods to register listener functions to an event and also to remove those listener functions, plus other various functions which mutates it's inner closed event object.

In addition to that all those core modules which fire events inherits from EventEmitter class thus gaining access to central event object. Thus, yes it does map functions to an object.

Upvotes: 1

Related Questions