nodejs的EventEmitter

EventEmitter

这里我观看了 @Richard Gong 的node.js高级编程03:events对象以及查阅书籍做出的简单模仿(ES6写法)。

构造

首先新建个名字为 EventEmitter 的 class 。
之后给其添加属性以及原型方法。

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

class EventEmitter {
constructor() {
this._events = {

}
}
emit(type) {
var funL = this._events[type];
var args = Array.from(arguments).slice(1);
funL.forEach(function(e) {
e(...args);
})
}
addListener(type, func) {
this._events = this._events || {};
if (!this._events[type]) {
this._events[type] = [];
}
this._events[type].push(func);
}
removeListener(type,func){
this._events[type].splice(this._events[type].indexOf(func),1);
}
on(type, func){
this.addListener(type, func)
}
}

上述代码中。onaddListener 本质上是一样的;

使用

首先实例化 EventEmitter 然后添加监听事件以及触发事件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

var emitter = new EventEmitter();
emitter.addListener('test', function(a1, a2) {
console.log(a1+a2);
});
emitter.on('test', function(a1, a2) {
console.log(a2);
});
emitter.emit('test', "augument1", "argument2");
emitter.emit('test', "augument3", "argument4");
//console
// augument1argument2
// argument2
// augument3argument4
// argument4

源码

源码

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×