观察者_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
class dataCenter {
/**
* 初始化
*/
constructor(fn_list=[],...args) {
this.fn_list = fn_list;
this.data = args
return new Proxy(this.data,this.handler)
}
/**
* handler 数据劫持
*/
handler = () => {
get: function(target, propKey, receiver) {
return Reflect.get(target, propKey, receiver);
},
set: function(target, propKey, value, receiver) {
this.cb(value,receiver)
return Reflect.set(target, propKey, value, receiver);
}
}
/**
* 注册回调->观察者
*/
ob=(fn)=>{
this.fn_list.push(fn)
}
/**
* 发布者
*/
cb=(newvalue,oldValue)=>{
const fn_list = this.fn_list;
for(var i in fn_list){
fn_list[i](newvalue,oldValue)
}
}
remove=(fn)=>{
//在当前回调列表移除该函数
}


}