-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwoSum.js
More file actions
36 lines (36 loc) · 804 Bytes
/
Copy pathtwoSum.js
File metadata and controls
36 lines (36 loc) · 804 Bytes
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
class EventEmitter {
constructor() {
this.eventMap = [];
}
on(type, handler) {
if (!type || !handler) {
throw new Error("参数错误");
}
if (!(handler instanceof Function)) {
throw new Error("handler is not function");
}
if (!this.eventMap[type]) {
this.eventMap[type] = [];
}
this.eventMap[type].push(handler);
}
emit(type, params) {
if (!type) {
throw new Error("参数错误");
}
if (this.eventMap[type]) {
this.eventMap[type].forEach((handler) => {
handler(params);
});
}
}
off(type) {
if (!type) {
throw new Error("参数错误");
}
if (this.eventMap[type]) {
let index = this.eventMap[type].indexOf(handle)
this.eventMap[type].splice(index, 1)
}
}
}