-
Notifications
You must be signed in to change notification settings - Fork 162
/
events.js
executable file
·45 lines (40 loc) · 1.24 KB
/
events.js
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
34
35
36
37
38
39
40
41
42
43
44
45
function EventEmitter(){
this._events = {};
}
EventEmitter.prototype.on = function(eventName,callback){
// if(eventName !== 'newListener'){
// this._events['newListener'] ? this._events['newListener'].forEach(fn => fn(eventName)) : void 0
// }
if(!this._events){
// this._events = {};这种方式有原型链
this._events = Object.create(null);
}
if(this._events[eventName]){
this._events[eventName].push(callback);
}else{
this._events[eventName] = [callback];
}
}
EventEmitter.prototype.once = function(eventName,callback){
function one(){
callback(...arguments);
this.off(eventName,one);
}
one.link = callback;
this.on(eventName,one);
}
EventEmitter.prototype.off = function(eventName,callback){
if(this._events[eventName]){
this._events[eventName] = this._events[eventName].filter(fn => {
fn != callback && fn.link != callback //为once做处理
})
}
}
EventEmitter.prototype.emit = function(eventName){
if(this._events[eventName]){
this._events[eventName].forEach(fn => {
fn.call(this,...arguments);
});
}
}
module.exports = EventEmitter;