Skip to content
Creates delegate functions automatically.
JavaScript
Find file
Latest commit ce52612 @joeferner Update README.md
Failed to load latest commit information.
test
.gitignore
README.md Update README.md
delegate.js
package.json

README.md

delegate

Creates delegate functions automatically. This is useful if you don't want to use util.inherits.

Example

Simple example.

var A = function() {
  this.evtEmitter = new delegate.EventEmitter();
  delegate(this, this.evtEmitter);
};

var obj = new A();
obj.on("test", function (data) {
  // read data
});

Example using class.js module.

var delegate = require('delegate');
var Class = require('class.js');

var MyEventEmitter = function () {
  events.EventEmitter.call(this);
};
util.inherits(MyEventEmitter, events.EventEmitter);

var A = Class({
  init: function () {
    this.evtEmitter = new MyEventEmitter();
    delegate(this, this.evtEmitter);
  }
});

var B = A.extend({
  emitDataEvent: function () {
    this.emit("data", 'hello world');
  }
});

var obj = new B();
obj.on("test", function (data) {
  // read data
});

API

delegate(destObj, srcObj)

Creates delegate functions for each function found on srcObj on destObj.

Arguments

  • destObj - The object you want the delegate function to exist on.
  • srcObj - The object you want to create delegate functions to.

delegate.EventEmitter

Convenience class the implements events.EventEmitter that you can delegate to.

Something went wrong with that request. Please try again.