-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path手写一个event.js
46 lines (45 loc) · 1.06 KB
/
手写一个event.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
46
class Event {
constructor() {
this._event = {}
}
on(type, cb) {
if(!this._event[type]) {
this._event[type] = []
}
if(typeof cb === 'function') {
// console.log(cb.name)
const index = this._event[type].findIndex(item => item.name && item.name === cb.name)
// console.log(index)
if(index >= 0) {
this._event[type].splice(index, 1)
}
this._event[type].push(cb)
}
}
off(type, cb) {
if(!this._event[type]) return
const index = this._event[type].findIndex(item => item.name && item.name === cb.name)
if(index >= 0) {
this._event[type].splice(index, 1)
}
}
emit(type, ...args) {
if(!this._event[type]) return
this._event[type].forEach(cb => {
cb(...args)
})
}
}
let e = new Event()
const fn = (...data) => {
console.log(data)
}
e.on('click', fn)
e.on('click', fn)
e.on('click', fn)
e.on('click', (e) => {console.log(e)})
e.on('click', () => {console.log(2)})
// e.off('click', fn)
// e.off('click', () => {console.log(1)})
console.log(e)
e.emit('click', 1111, 11)