-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEventEmitter.js
41 lines (36 loc) · 1.05 KB
/
EventEmitter.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
class EventEmitter {
constructor() {
// 事件对象,存放订阅的名字和事件,例如:{click: [handle1, handle2]}
this.events = {};
}
// 订阅事件的方法
on(eventName, callback) {
if (!this.events[eventName]) {
// 一个名字可以订阅多个事件函数
this.events[eventName] = [callback];
} else {
this.events[eventName].push(callback);
}
}
// 触发事件的方法
emit(eventName, ...rest) {
// 遍历执行所有订阅的事件
this.events[eventName] &&
this.events[eventName].forEach((f) => f.apply(this, rest));
}
// 移除订阅事件
remove(eventName, callback) {
if (this.events[eventName]) {
this.events[eventName] = this.events[eventName].filter(
(f) => f != callback
);
}
console.log(this.events);
}
}
const event1 = new EventEmitter();
const handle = (...pyload) => console.log(pyload);
event1.on("click", handle);
event1.emit("click", 100, 200, 300, 100);
event1.remove("click", handle);
event1.emit("click", 100, 200, 300, 100);