-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobserver.js
More file actions
37 lines (36 loc) · 972 Bytes
/
observer.js
File metadata and controls
37 lines (36 loc) · 972 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
37
/*
* @Author: yeyu98
* @Date: 2024-03-08 20:53:33
* @LastEditors: yeyu98
* @LastEditTime: 2024-03-08 21:15:13
* @Description:
*/
class Observer {
clientList = []
listen(fn) {
this.clientList.push(fn)
}
trigger(...args) {
if(!this.clientList.length) return
for(let i=0; i<this.clientList.length; i++) {
const fn = this.clientList[i]
fn && fn(...args)
}
}
remove(fn) {
if(!this.clientList.length) return
for(let i=0; i < this.clientList.length; i++) {
const _fn = this.clientList[i]
if(_fn === fn) {
this.clientList.splice(i, 1)
}
}
}
}
const observer = new Observer()
observer.listen(function(price, squareMeter) {
console.log(`房价${price}`)
console.log(`面积${squareMeter}`)
})
observer.trigger('200万', '50平米')
observer.trigger('100万', '25平米')