Skip to content

Instantly share code, notes, and snippets.

@jmar777
Created November 27, 2010 23:00
Show Gist options
  • Save jmar777/718366 to your computer and use it in GitHub Desktop.
Save jmar777/718366 to your computer and use it in GitHub Desktop.
The EventEmitter appears to pass a minimum of two arguments to any bound event handlers, regardless of how many arguments are passed to the emit() function.
// using node v0.2.4
var sys = require('sys'),
events = require('events');
// create a custom EventEmitter
function CustomEmitter() {
// call "super" constructor
events.EventEmitter.call(this);
}
// inherit from EventEmitter
sys.inherits(CustomEmitter, events.EventEmitter);
// create a test instance
var test = new CustomEmitter();
// bind a handler to the 'foo' event
test.on('foo', function() {
// log the arguments
console.log(arguments);
});
// run some tests:
test.emit('foo');
// logs 2 args: { '0': undefined, '1': undefined }
test.emit('foo', 'bar');
// logs 2 args: { '0': 'bar', '1': undefined }
test.emit('foo', 'bar', 'baz');
// logs 2 args: { '0': 'bar', '1': 'baz' }
test.emit('foo', 'bar', 'baz', 'hello', 'world');
// logs 4 args: { '0': 'bar', '1': 'baz', '2': 'hello', '3': 'world' }
@jmar777
Copy link
Author

jmar777 commented Nov 28, 2010

I agree. It was suggested in the IRC channel (by webr3) to simply handle the "fast cases" (0-5 arguments or so) with a switch/case, and then do the Array.prototype.slice for higher numbers of arguments. I generally like the idea - think it would make sense to run some actual tests though to see where the hand-off should occur.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment