dima
dima

Reputation: 910

Passing a callback function to a prototype

Experimenting with a messaging module that will provide methods for emitting and listening to events between several modules in a Node app. Fairly new to this endeavor. My goal is to initialize a listener and have it feed directly into a callback function.

Getting tripped up on syntax and JS prototyping at this point.

var events = require('events');
var inter_com = new events.EventEmitter();


var com_bus = {
  initListener:function(listener_name, callback){
    this.listener_name = listener_name;
    this.callback_function = callback;
    inter_com.on(this.listener_name, this.callback_function);
  },
  sendMessage:function(data){
    inter_com.emit(this.listener_name,data);
    console.log('test event sent');
  }
}

var instance_of_com = Object.create(com_bus);
instance_of_com.initListeners('testing',pipeMe);
instance_of_com.sendMessage('its good to be the king');


var pipeMe = function(data){console.log(data)};`

Upvotes: 1

Views: 547

Answers (2)

sapy
sapy

Reputation: 9592

JavaScript follows prototypal inheritance ,

1) To inherit from an object , parent need to be a Function.Convention is that function should be named as InitCap , that denotes it's a constructor.

2) Also child inherits the prototype so instead of inheriting from Function_Name you need to inherit from Function_Name.prototype.

var events = require('events');
var inter_com = new events.EventEmitter();

function Com_bus() {

}

Com_bus.prototype.initListeners = function(listener_name, callback) {
    this.listener_name = listener_name;
    this.callback_function = callback;
    inter_com.on(this.listener_name, this.callback_function);
};

Com_bus.prototype.sendMessage = function(listener_name, callback) {
    inter_com.emit(this.listener_name, data);
    console.log('test event sent');
};

var pipeMe = function(data) {
    console.log(data)
};

var instance_of_com = Object.create(Com_bus.prototype);
instance_of_com.initListeners('testing', pipeMe);
instance_of_com.sendMessage('its good to be the king');

Upvotes: 1

jmunsch
jmunsch

Reputation: 24099

one way to solve this is to create a factory which returns an instance of com_bus:

function returnComBusInstance () {
    // create a closure on these variables
    var listener_name;
    var callback_function;
    return {
        initListener:function(_listener_name, _callback){
            listener_name = _listener_name;
            callback_function = _callback;
            inter_com.on(listener_name, callback_function);
        },
        sendMessage:function(data){
             inter_com.emit(listener_name, data);
             console.log('test event sent');
        }
    }
}

and then:

var com_bus_instance = returnComBusInstance()

I personally try to avoid using this but if you need to then you can either, bind, call, or apply, or again you could create a factory but create a closure on the context of this

Or you could pass in prototype properties to Object.create:

Object.create(com_bus, {
    'listener_name': {value: null, enumerable:true},    
    'callback_function': {value:null, enumerable:true}
});

Upvotes: 1

Related Questions