blob: 6845f2a7c8f3650cb906310e8625bca154d70641 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
var events = require('events');
var util = require('util');
function MyEventEmitter() {
if (false === (this instanceof MyEventEmitter))
return new MyEventEmitter();
/* inherit all prototype objects, methods, etc */
events.EventEmitter.call(this); /* hmm this calls a ctor? notice it's not a method */
}
/* ensure that the prototype methods of the specified superCtor are inherited into ctor */
util.inherits(MyEventEmitter, events.EventEmitter);
MyEventEmitter.prototype.poke = function(msg) {
this.emit('poke', 'poking ' + msg)
}
MyEventEmitter.prototype.yell = function(msg) {
this.emit('poke', 'yelling at ' + msg)
}
var ee = new MyEventEmitter;
ee.on('poke', function(msg) {
console.log(msg);
})
.on('yell', function(msg) {
console.log(msg);
});
ee.poke('kyle');
ee.yell('bro');
|